1

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?

user3475234
  • 1,503
  • 3
  • 22
  • 40

4 Answers4

2

It should be something like this:

document.querySelectorAll("[id^=hide]")
chriss
  • 669
  • 4
  • 9
1

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"
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
1

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

$( "li[id^='hide']" ).each(function(){
   $(this).hide();
});
Seth Pollack
  • 269
  • 2
  • 13
-1
document.querySelectorAll('[id^="hide"]');
Pawel.W
  • 166
  • 1
  • 1
  • 8