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
Comments
Post a Comment