I have lots of span tags in the document. I want give span element id which has "check" text inside and it should start with 0 like: fiddle
<span>not check</span>
<span>not check</span>
<span id="0">check</span>
<span id="1">check</span>
<span id="2">check</span>
<span>not check</span>
<span id="3">check</span>
<span id="4">check</span>
My JavaScript code is as follows:
$(function(){
$('span').each(function(i){
$(this).filter(function(){
if( $(this).text().trim()=='check'){
$(this).attr('id',i);
$(this).attr('class',$(this).index())
}
})
})
})