PhilD
PhilD

Reputation: 277

getting text from second child span with jquery

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

Answers (2)

jcvegan
jcvegan

Reputation: 3170

Or this:

$("#dir li").click(function(){
   $("#target").val($($(this).children()[1]).text());
});

Upvotes: 2

tymeJV
tymeJV

Reputation: 104775

Use .find()

var text = $(this).find("span").text();
$("target").val(text);

Upvotes: 4

Related Questions