Reputation: 75
How do i get a value of a nested tag ?
I want to access the value "MY NAME IS JOHN" using jquery, how do i do that
<ul>
<li id="name_check"><a href="#"> MY NAME IS JOHN </a></li>
</ul>
How do i use jquery and the given id of 'li' to access this name .
I tried using jQuery(#name_check).value, but it give "0" as the result on a javascript alert box
Upvotes: 2
Views: 1135
Reputation: 59
var a = jQuery(#name_check a).text()
or
var a = jQuery(#name_check).find(a).text()
Upvotes: 0
Reputation: 11
like this
<a id="test">test text</a>
alert($("#test").html())
example http://jsfiddle.net/Elfego/KUHxn/1/
Upvotes: 0
Reputation: 5490
You should use -
$("#name_check").find('a').text(); //recomended
the other option is -
$("#name_check a").text(); //not recomended
but I will suggest you to avoid this below one as per jQuery docs find works faster in finding child to specific parent instead of writing in css way
Upvotes: 0
Reputation: 2988
var content = $('#name_check a').html();
It's like the selectors you know from css: with the space after the #name_check you say that you want the <a>
tag inside of it.
Upvotes: 1
Reputation: 6608
You could get the text value with
$('li#name_check > a').text();
as shown here: http://jsfiddle.net/uanS4/
Upvotes: 2
Reputation: 56439
Try using:
$("#name_check a").text();
Note the space between #name_check
and a
. That means any a
tag that is a child (at any level of the DOM) of #name_check
Upvotes: 6