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
source share
3 answers

Your function will look something like this:

var addItem = function(item){
    $("#searchListUl").append('<li data-filtertext="'+item+'"><a href="#">'+item+'</a></li>');
}

You can call him: addItem("apple")

+13
source

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
source

, .append() , , Cross Site Scripting (XSS). , , HTML-, .append(), HTML DOM. Script. javascript someones, , , .

.text(), . jquery,.text() HTMLEncode, . SQL, .append($ RANDOM_STRING) , conn.exec($ RANDOM_STRING). .

, , , . , ,.append() HTML, , HTML DOM.

-2

All Articles