-1

I have this line of JS which I have determined is wrong.

classes[i] = document.getElementsByAttribute ("class", show_hide_class_selectors[i]);

In context

for (var i = 0; i< show_hide_class_selectors.length; i++) {
        classes[i] = document.getElementsByAttribute ("class", show_hide_class_selectors[i]);
        alert ("ok");
    }

Can someone see where this is wrong?

1
  • 1
    Do you know getElementsByAttribute only works on XUL elements, not standard W3C DOM elements? Commented Jun 29, 2011 at 10:44

4 Answers 4

5

You invented a non-standard getElementsByAttribute method on the document object, and the code is failing because it doesn't exist.

You should probably looking at using a selector engine; every major JS library includes one, and there are a number of tiny implementations you can use.

Sign up to request clarification or add additional context in comments.

2 Comments

+1, You should also suggest a library that simplifies finding elements with a certain class. Or suggest a modern browser that supports getElementsByClassName.
Apparently its been longer than I thought since I used Javascript :) :(
1
for (var i = 0; i< show_hide_class_selectors.length; i++) {
    classes[i] = document.getElementsByClassName (show_hide_class_selectors[i]);
    alert ("ok");
}

if however won't work in IE 6-8. Better use jQuery or another library, providing css selectors for JS

Comments

0

There are numerous implementations for this function on the internet (example), but these are cusome implementations that expand the document object.

You can add it in yourself by placing it in your code.

Better still though, if you are wanting to do this sort of thing, then use a JavaScript library such as jQuery this handles all of this for you.

Comments

0

Erm, close, I suppose. You want getElementsByClass.

for (var i = 0; i < show_hide_class_selectors.length; i++) {
  classes[i] = document.getElementsByClass(show_hide_class_selectors[i]);
  alert("ok");
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.