Sri
Sri

Reputation: 1505

get id of an li with the text value in jquery

I have a code

<li id="a1">Apple</li>

What I need to do is with the text value "Apple" as input, I need to find the id of that li.

Is this possible in javascript / jquery?

Thanks in Advance ! ! !

Upvotes: 0

Views: 746

Answers (2)

Sadikhasan
Sadikhasan

Reputation: 18600

You need to use contains() with search string like

alert($("li:contains('Apple')").attr("id"));

Demo

Upvotes: 2

dewd
dewd

Reputation: 4429

using jquery get all li's and loop through them to find the text you're after. then if you find a match grab the id.

$(function(){

   var lis = $('li');

   $(lis).each(function(){
       if ($(this).text() === 'Apple')
          alert($(this).attr('id'));
   });

});

here's a fiddle example: http://jsfiddle.net/3L5wnp2q/

Upvotes: 2

Related Questions