Reputation: 277
Trying to get text from nested span(which is the second of two elements in an li
) into an input. my function isnt working not sure why.
html
<ul id="dir"><li><img><span>TEXT TO GET</span></li></ul>
<input type="text" id="target">
jQuery
$("#dir li").click(function(){
$("target").val($(this).children[1].text());
});
Upvotes: 1
Views: 2127
Reputation: 3170
Or this:
$("#dir li").click(function(){
$("#target").val($($(this).children()[1]).text());
});
Upvotes: 2
Reputation: 104775
Use .find()
var text = $(this).find("span").text();
$("target").val(text);
Upvotes: 4