Reputation: 47
I'm having a bit of trouble dynamically adding items to a listview in jQuery mobile. Basically I want whatever is input by the user in the textbox to be added to the list. I have the following code and can not figure out why the desired output is not appearing.
<script>
var listCreated = false;
function appendToList() {
if(!listCreated) {
$("#items").append("<ul id='list' data-role='listview' data-inset='true'></ul>");
listCreated = true;
$("#items").trigger("create");
}
$("#list").append("<li>");
$("#list").append(document.getElementById(item).value);
$("#list").append("</li>");
$("#list").listview("refresh");
}
</script>
<div data-role="content">
<div id="items"></div>
<input type="text" id="item" />
<input type="button" value="Add item to list" onclick="appendToList()"/>
</div>
Upvotes: 3
Views: 11795
Reputation: 104775
Try creating the entire li
then appending, right now, you're appending an opening <li>
to the list, then the value to the list, not the new li
var value = $("#item").val();
var listItem = "<li>" + value + "</li>";
$("#list").append(listItem);
Demo: http://jsfiddle.net/DVbGY/1/
Upvotes: 3