0

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.

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

6 Comments

I suppose OP needed $('#strSearch').val(this.attr('value));
Not sure what I am doing wrong. The function itself isn't being called when I click an item.
Probably because you're appending the list items to dbsearch and not list @MickB
@MickB see the fiddle
I tried your changes and now my list won't display. Do I need to close my ul tag at the end?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.