how can i get the id of an element by using the find method?
HTML
<div id="d1" class="line1">blib blab blub </div>
JS
$(function() {
// hit_id = $(".line1").find("blab").attr('id');
hit_id = $(".line1").match(/blab/).attr('id');
$('body').append(' - '+hit_id+' - ');
});
Working example
http://www.jsfiddle.net/V9Euk/496/
Thanks in advance!
Peter
You can use
:contains()for the text match, then.attr('id')like you have for the ID, like this:You can see the updated/working version here.
.match()isn’t a jQuery function unless you’re adding some plugin (see the console on your example page for the error:Object...has no method 'match').:contains()will reduce the element set to those that have text containing what you pass as the string to:contains()though, which seems to be what you’re after.