rsharma
rsharma

Reputation: 75

Accessing value inside "a" tag , which is inside "li" tag?

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

Answers (8)

Aleks Sekowski
Aleks Sekowski

Reputation: 59

var a = jQuery(#name_check a).text()

or

var a = jQuery(#name_check).find(a).text()

Upvotes: 0

Elfego
Elfego

Reputation: 11

like this

<a id="test">test text</a>


alert($("#test").html())

example http://jsfiddle.net/Elfego/KUHxn/1/

Upvotes: 0

Rohit Agrawal
Rohit Agrawal

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

alianos-
alianos-

Reputation: 916

jQuery("#name_check a").html();

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122008

Try with

 var val= $('#name_check a').text();

Upvotes: 0

Michael Kunst
Michael Kunst

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

ayyp
ayyp

Reputation: 6608

You could get the text value with

$('li#name_check > a').text();

as shown here: http://jsfiddle.net/uanS4/

Upvotes: 2

Mathew Thompson
Mathew Thompson

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

Related Questions