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
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
Share asked May 30, 2013 at 16:03 rsharmarsharma 751 silver badge9 bronze badges8 Answers
Reset to default 6Try 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
You could get the text value with
$('li#name_check > a').text();
as shown here: http://jsfiddle/uanS4/
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.
Try with
var val= $('#name_check a').text();
jQuery("#name_check a").html();
You should use -
$("#name_check").find('a').text(); //reended
the other option is -
$("#name_check a").text(); //not reended
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
like this
<a id="test">test text</a>
alert($("#test").html())
example http://jsfiddle/Elfego/KUHxn/1/
var a = jQuery(#name_check a).text()
or
var a = jQuery(#name_check).find(a).text()
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744925305a4601396.html
评论列表(0条)