Question

I know getElementsByName('something') that returns the elements with name="something", but I want to return a list of elements where custom="something", how would I do that?

Was it helpful?

Solution 2

To answer my own question, it seems it was easier than I thought.

elements = document.getElementsByTagName('pre');

for (elem = 0;elem < elements.length;elem++)
 {
  element = elements[elem];

  if (element.lang != 'php')
   break;
  ...
 }

The above happened to work in my situation. :)

OTHER TIPS

There are no standard API in the DOM to do this.

If you do not mind adding jQuery to your project, you could query your elements using the jQuery attribute selector:

$("[custom='something']")

This page lists all the functions of the Document object in the JavaScript available in browsers. Thus it provides getElementById(), getElementByName() and getElementByTagName().

I guess need to use something like JQuery to gain more freedom as it allows you to express more complex "queries". I'm not sure, but that might also be slower, depending on how often you have to look up things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top