javascript - Accessing value inside "a" tag , which is inside "li" tag? - Stack Overflow

How do i get a value of a nested tag ?I want to access the value "MY NAME IS JOHN" using jque

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 badges
Add a ment  | 

8 Answers 8

Reset to default 6

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

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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信