Reputation: 1505
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
Reputation: 18600
You need to use contains() with search string
like
alert($("li:contains('Apple')").attr("id"));
Upvotes: 2
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