I wrote a script to show or hide items in an HTML list (<ul
id="stuff">) depending on whether a list-item's CLASS
attribute matches an input string (catString), which is
chosen from a SELECT menu.
Can anyone suggest any improvements to this (below)? I'm
a JS beginner.
It seems to work Camino/Firefox and Mac Explorer-5, but not
in Safari. I haven't been able to test it in
Explorer/Windows. You can test it here if you like:
Any advice is welcome. Thanks.
function showCat(catStri ng){
// show all elements w/ class attribute matching catString
var stuff =
document.getEle mentById("stuff ").getElementsB yTagName("li");
// get all LIst elements in ul#stuff
for (var i in stuff){
if (stuff[i].className){
// if catString matches OR if show "everything " then
if (stuff[i].className.inde xOf(catString) >= 0 ||
catString == "everything "){
// show this block
stuff[i].style.display = "block";
}
else {
// hide this block
stuff[i].style.display = "none";
}// (className exists?)
}// (className matches?)
}// (for i in stuff)
}// end showCat
function showCatFromSele cted(){
// call showCat using the selected OPTION's value
showCat(this.op tions[this.selectedIn dex].value);
}
function handleSelect(){
// handle the onchange event
document.getEle mentById("show-me").onchange =
showCatFromSele cted;
}
window.onload = handleSelect;
id="stuff">) depending on whether a list-item's CLASS
attribute matches an input string (catString), which is
chosen from a SELECT menu.
Can anyone suggest any improvements to this (below)? I'm
a JS beginner.
It seems to work Camino/Firefox and Mac Explorer-5, but not
in Safari. I haven't been able to test it in
Explorer/Windows. You can test it here if you like:
Any advice is welcome. Thanks.
function showCat(catStri ng){
// show all elements w/ class attribute matching catString
var stuff =
document.getEle mentById("stuff ").getElementsB yTagName("li");
// get all LIst elements in ul#stuff
for (var i in stuff){
if (stuff[i].className){
// if catString matches OR if show "everything " then
if (stuff[i].className.inde xOf(catString) >= 0 ||
catString == "everything "){
// show this block
stuff[i].style.display = "block";
}
else {
// hide this block
stuff[i].style.display = "none";
}// (className exists?)
}// (className matches?)
}// (for i in stuff)
}// end showCat
function showCatFromSele cted(){
// call showCat using the selected OPTION's value
showCat(this.op tions[this.selectedIn dex].value);
}
function handleSelect(){
// handle the onchange event
document.getEle mentById("show-me").onchange =
showCatFromSele cted;
}
window.onload = handleSelect;
Comment