Using Prototype to find elements by id and toggle them

written by justin on January 29th, 2008 @ 04:49 PM

Today I needed to toggle the view of some elements on an html page. I was trying to find them by id, as I was generating an id that started with an uppercase n like so "N". I found that Prototype has a nice way of doing it. So here it is:

function toggle_n() {
        // this finds all elements that have an id that begins with N      
	var a = $$("[id^=N]");
	
        // iterate the array and toggle each id
	a.each(function(num) {
	  Element.toggle(num);
	}); 
}

Post a comment