Chetana Kestikar
Chetana Kestikar

Reputation: 570

Get the id of <li> tag containing a specific value

I have a list as:

 <ul>
 <li id="l1">1</li>
 <li id="l2">2</li>
 <li id="l3">hi</li>
 </ul>

By using jquery, i got to know if any <li> contains value(say) hi. For that i used:

if($('li:contains('hi')')){
    alert('Got value');
}

Now i want to know the id of the <li> in which i found value 'hi'. How do i get that id?

Upvotes: 2

Views: 1295

Answers (4)

arulmr
arulmr

Reputation: 8846

Use this:

var id = $('li:contains("hi")').attr('id');

JSFIDDLE

Upvotes: 3

Neophile
Neophile

Reputation: 1519

var id = jQuery('li:contains("hi")').attr('id');

Upvotes: 1

mariano
mariano

Reputation: 1367

var id = $('li:contains("hi")').attr('id');

Upvotes: 3

CappY
CappY

Reputation: 1580

Well the ID is simple attribute, you could do:

.attr("id")

Upvotes: 1

Related Questions