getElementsByName Wildcard

Good evening guys,

I would like to ask is there any way I can use document.getElementsByName(“polls-*”) and it will get all elelments which starts with the name “polls-” ?

Thanks in advanced.

Hi,

When you say start with the name “polls-”, do you mean the ID? e.g. <div id=“polls-1”>?

If so, then yes, it’s possible - but you’d need a JavaScript function to do it. That function would have to recurse the DOM, check the ID against a regular expression, and return a collection (or array) of valid nodes.

We can certainly help you with that - please get in touch if you are interested.

hi there,

Yea that is what I meant, I also thought of using the recuse way, I thought there might be an easier way out.

Thanks =)

I would like to ask is there any way I can use document.getElementsByName(“polls-*”)

No.

Presumably there are seperations of possibliity to this question?

In theory (and probably practice) it would be possible to emulate such a behaviour, but it would involve checking EVERY SINGLE ELEMENT firstly for the presence of the attribute, then running a dynamically compiled reg exp.

GamerZ, in reality i would follow with 7Stud, practically this is not possible, or more so, this should not be possible.

Could we ask what functioanality it is you are trying to achieve, and maybe someone can suggest an alternative methodology?

Many thanks

/Matt

I suppose you could limit the recursion by using getElementsByTagName, and then parsing that collection for the name=“” part? (Assumes that all elements you seek have the same tag type e.g. <div> )

It has been noted before, but perhaps it should be again: the name attribute is supported only by a limited set of elements. The div element is not among those.

Recursing every page element is only a problem if you have a really, really long page. Even then, the work would be done quickly. Most JS libraries are provided with a getElementByClass type of function which does it.

In this case, GamerZ, you can limit the recursion by:

  1. Starting at a node deeper in the DOM, e.g. if you know that all the elements are contained within, say, <div id=“allposts”>.

  2. Only examining DIVs for the correct name, rather than any node.

  3. Ignore processing of any text, attributes, comments, or other non-element nodes.

Most Javascript libraries allow you to traverse your documents using many easy to understand selectors, for instance, take a look at jQuery selectors, which supports the XPath-like syntax E[@foo^=bar].

Of course you could write your own Javascript functions but most times a solid library will be more than enough.

Getting a library such as Prototype or Yahoo! UI is an option, but they can be huge and will add a lot of weight to your pages. Only use them if you intend to make use of several other facilities.

There’s no real substitute to writing your own JS libraries. You’ll find it easier to debug your own script, you’ll learn a lot, and then there’s the enormous sense of satisfaction!

In this particular case, the function could be written in a couple of dozen lines.

I believe both Prototype and Yahoo! UI are both pretty big, but jQuery is under 20KB (trimmed)! It also manages to pack a lot of functionality into it.

Agree, jQuery is pretty small and focus on very simple tasks, not the great effects we see everywhere these days but gets the job done, it can do what was asked in this thread :slight_smile: