Search code examples
javascriptjqueryhtmllistunordered

How to select item from unordered list jquery


I have a function that fills my unordered list.

$("#DBSearch").append('<ul id="List" data-role="listview">');


for(var i = 0; i < 10; i++)
{           
    $("#DBSearch").append("<li value=" + i + "><a href="+ "#" + ">" + obj.Search[i].Title + "</a></li>" );      
}

When I click on an item in the list I want my text box strSearch to fill with that item.


Solution

  • When you click a li in list dbsearch it will replace the text with strSearch

    $("#List").on("click", "li", function() {
        $("#strSearch").val($(this).text());
    });
    

    If you want to replace the text of the nested a link use $("a", this).text(...).

    Also a note, generally you shouldn't capitalize ids/classes in html

    Update: here's a demo fiddle