javascript - get all elements that start with "hide"

1.5k views Asked by At

Essentially, I'd like to hide certain list items in a variable size list of elements on the click of a button.

getElementById doesn't really serve the purpose by itself, because I need to hide all the list elements whose id starts with "hide". So for example, I need to hide li#hide1, li#hide2, etc. Any ideas as to how to go about this?

4

There are 4 answers

0
Roko C. Buljan On BEST ANSWER

jsFiddle demo

var liHide = document.querySelectorAll("[id^=hide]");

for(var i=0; i<liHide.length; i++){
   // do someghing with liHide[i] like:
   liHide[i].style.display = "none";
}

If you use jQuery you can do it simply like:

$( "li[id^=hide]" ).hide(); // Hide all LI which ID starts with "hide"
0
chriss On

It should be something like this:

document.querySelectorAll("[id^=hide]")
0
Seth Pollack On

If you are using jQuery you can do something like this:

$( "li[id^='hide']" ).each(function(){
   $(this).hide();
});
0
Pawel.W On
document.querySelectorAll('[id^="hide"]');