javascript - Using jQuery to search through element attribute values -


i'm trying make filter on screen, hiding doesn't meet requirements.

this i've come far. im not sure doing wrong

jquery :

jquery('#searchbox').on('keyup change', function() {     var search = $('#searchbox').val();       //for each h4            $('h4').each(function(){      var h4id = $(this).attr('id');         if ($(h4id).contains(search))             $(this).show();          else             $(this).hide     }); 

html :

<input type="search" name="search" id="searchbox"/>  <h4 id="adminstrator">administrator</h4>  <h4 id="john,smith">john smith</h4>  <h4 id="jane,smith">jane smith</h4>   

(i'm using jquery 1.9.1) (so, if start typing smith, "administrator" h4 should disappear.

.contains not give text content of selector. search elements inside selector.

try approach .. can lot more optimized

jquery('#searchbox').on('keyup change', function () {     var search = $('#searchbox').val().tolowercase();      $('h4').hide();     $('h4').each(function () {         var h4id = $(this).attr('id'),             $text = $('#'+ h4id).text().tolowercase();         if($text.indexof(search) > -1)              $(this).show();     });  });  

make sure id's unique.

next id's should not contain , in them

jsfiddle


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -