javascript - Elements with display set to none are shown -


i have created following greasemonkey script firefox filter class names in long list of java api reference :

// ==userscript== // @name        jdk api doc helper problematic // @version     1 // @namespace   xolve // @description provides in place search jdk api docs // @include     http://docs.oracle.com/javase/7/docs/api/* // @run-at document-end // ==/userscript==  var classnamesframe; var classnamesdoc; var classnameshash = {}; var timeoutid;  function textboxtextchanged(ev) {     window.cleartimeout(timeoutid);     console.log(ev.target.value);     // case insensitive comparision     var query = string(ev.target.value).trim().tolowercase();     timeoutid = window.settimeout(filterclassnames, 600, query);  }  function filterclassnames(query) {     if(query.length == 0) {         for(k in classnameshash) {             classnameshash[k].style.display = "";         }         return;     }     for(k in classnameshash) {         if(k.startswith(query)) {             console.log("setting display: " + k);             classnameshash[k].style.display = "block";         }         else {             classnameshash[k].style.display = "none";         }     } }  function init() {     // find list of class names     classnamesdoc = classnamesframe.contentdocument;     classnamesatags = classnamesdoc.getelementsbytagname("a");     for(i = 0; < classnamesatags.length; i++) {         // case insensitive comparision         classnameshash[classnamesatags[i].textcontent.tolowercase()] = classnamesatags[i];     }      // add text box     var textbox = classnamesdoc.createelement("input");     var body = classnamesdoc.getelementsbytagname("body")[0];     var classlistnode = body.getelementsbyclassname("indexcontainer")[0];     body.insertbefore(textbox, classlistnode);     textbox.addeventlistener("keyup", textboxtextchanged); }  classnamesframe = document.getelementsbyname("packageframe")[0]; classnamesframe.onload = init; 

this script does

  1. add text box frame lists class names. call query box.

  2. create hash of class names (converted lower case) corresponding dom element.

  3. when user enters first few characters of class name in query box, loops through hash keys , sets display property of matching elements "block" , display property of non-matching element "none"

problem

some elements displayed e.g. aclentry, array etc. irrespective of contents of query box,

it's because there multiple "aclentry" links, example. so, when first add elements classnameshash array, overwrite 1st "aclentry" link 2nd 1 because id you're using same.

you need append unique id , since you're checking .startswith match should still work fine.

for example:

for(i = 0; < classnamesatags.length; i++) {     // case insensitive comparision     classnameshash[classnamesatags[i].textcontent.tolowercase() + '_' + i] = classnamesatags[i]; } 

this append _# end of id inside array , should keep them unique have aclentry_34 , aclentry_35 or that. when try filter out acl* match both of them.

conversely, all links in classnameshash object, all unmatching links can hidden.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -