How to add dynamically <li> tags to jquery list?
At that moment, when I can add li tags to my list using a script. But how to add dynamically li tags to a function in .js? I hope I see a good example. Below is my code. Thank!
<div data-role="page" id="searchPage" data-theme="b">
<div data-role="content">
<ul data-role="listview" data-filter="true" data-theme="b" id="searchListUl">
</ul>
</div>
<script type="text/javascript">
$("#searchListUl").append('<li data-filtertext="Apple"><a href="#">Apple</a></li>');
$("#searchListUl").listview('refresh');</script></div>
+5
3 answers
If you with “dynamic” mean you want to add list items without knowing the name (“Apple”), you can create a generic function using the jQuery function used to create the elements:
function add(name) {
var $li = $("<li>").attr("data-filtertext", name)
.appendTo("#searchListUI");
$("<a>").attr("href", "#")
.text(name)
.appendTo($li);
}
You can use it as follows:
<div data-role="page" id="searchPage" data-theme="b">
<div data-role="content">
<ul data-role="listview" data-filter="true" data-theme="b" id="searchListUl">
</ul>
</div>
<script type="text/javascript">
add("Apple");
$("#searchListUl").listview('refresh');</script></div>
0