javascript - jQuery search bar only working one time per page -
i working on page pre-registration events on website. on page, people need ability add names many slots event creator (so needs handle 3 person basketball teams , 50 person banquets). have had high quality facebook-like search bar made can neatly search through database , select desired people. sadly have search bar being created loop creates many different id filled search bars every 1 of them left empty , first search bar 1 filled.
i found deals jquery code @ top of page. question/issue need jquery work on multiple search bars on single page. if can me accomplish i'd appreciative.
the "top code" or jquery code pulls db is:
$(function(){ $(".search").keyup(function() { var inputsearch = $(this).val(); var datastring = 'searchword='+ inputsearch; if(inputsearch!='') { $.ajax({ type: "post", url: "../searchmychap.php", data: datastring, cache: false, success: function(html) { $("#divresult").html(html).show(); } }); }return false; }); jquery("#divresult").live("click",function(e){ var $clicked = $(e.target); var $name = $clicked.find('.name').html(); var decoded = $("<div/>").html($name).text(); $('#inputsearch').val(decoded); }); jquery(document).live("click", function(e) { var $clicked = $(e.target); if (! $clicked.hasclass("search")){ jquery("#divresult").fadeout(); } }); $('#inputsearch').click(function(){ jquery("#divresult").fadein(); }); }); </script> <style type="text/css"> body{ font-family: 'lucida grande', tahoma, verdana, arial, sans-serif; } .contentarea{ width:600px; margin:0 auto; } /* #inputsearch { width:350px; border:solid 1px #000; padding:3px; } */ #divresult { position:absolute; width:545px; display:none; margin-top:-1px; border:solid 1px #dedede; border-top:0px; overflow:hidden; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; -moz-border-bottom-right-radius: 6px; -moz-border-bottom-left-radius: 6px; box-shadow: 0px 0px 5px #999; border-width: 3px 1px 1px; border-style: solid; border-color: #333 #dedede #dedede; background-color: white; } .display_box { padding:4px; border-top:solid 1px #dedede; font-size:12px; height:50px; } .display_box:hover { background:#0088cc; //background:#3bb998; color:#ffffff; cursor:pointer; }
the for-loop code prints search bars follows:
for($i = 0; $i < $looper; $i++) { echo' <div class="row"> <div class="form-group"> <div class="col-md-12"> <label>member name:</label> <input type="text" class="form-control search" name="member'.$i.'" autocomplete="off" id="inputsearch" placeholder="search..."> <div id="divresult" style="z-index:999; margin-top: 35px;" ></div> </div> </div> </div>'; }
edit: working jsfiddle
the first issue each iteration of for
loop element created id="divresult"
. id should used once in whole document. have changed for
loop produce element class="divresult"
instead. if use change, remember css need changed accordingly.
for ($i = 0; $i < $looper; $i++) { echo ' <div class="row"> <div class="form-group"> <div class="col-md-12"> <label>member name:</label> <input type="text" class="form-control search" name="member'.$i.'" autocomplete="off" id="inputsearch" placeholder="search..."> <div class="divresult" style="z-index:999; margin-top: 35px;"></div> </div> </div> </div>'; }
next iterate on each .search
element. within each iteration can find corresponding 'result' element using jquery's next()
function, retrieves following sibling of element. if code ever changed such 'results' element not appear straight after `.search' element, need changing.
$(function () { $('.search').each(function(index) { var $searchelement = $(this); var $resultelement = $searchelement.next(); console.log(index, $searchelement, $resultelement); $searchelement.on('keyup', function() { var inputsearch = $searchelement.val(); var datastring = 'searchword=' + inputsearch; if (inputsearch != '') { $.ajax({ type: "post", url: "../searchmychap.php", data: datastring, cache: false, success: function (html) { $resultelement.html(html).show(); } }); } return false; }); $resultelement.on("click", function (e) { var $clicked = $(this); var $name = $clicked.find('.name').html(); var decoded = $("<div/>").html($name).text(); $searchelement.val(decoded); }); $(document).on("click", function (e) { var $clicked = $(e.target); if (!$clicked.hasclass("search")) { $resultelement.fadeout(); } }); $searchelement.on('click', function () { console.log(index + ' clicked'); $resultelement.fadein(); }); }); });
Comments
Post a Comment