views:

45

answers:

3

Hi,

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?

+1  A: 

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']")
Vincent Robert
I rather not, because it adds one more HTTP download for the client just for this single feature... but thanks for the suggestion anyway.
rFactor
If you are concerned about the number of HTTP downloads, then you are already concatenating all your JS files into one, are you?. Adding another one for jQuery should not be a problem.
Vincent Robert
A: 

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.

BastiBense
+1  A: 

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. :)

rFactor