tags:

views:

116

answers:

4

Is there a way to use getElementsByName without starting from the DOM root.

For example I have a div element, and want to start searching from that element.

If not, then do I have to write my own function that recursively iterates through the child nodes, or is there a different way to do it.

A: 
Pointy
He's talking about `getElementsByName` not `getElementsById`
Josh
Ah. Well ok then! Carry on.
Pointy
A: 

have you tried.

document.getElementsbyTagName("div")[0].getElementsbyName("your search");

also with jQuery selectors the search for dom elements become quite easier.

sushil bharwani
+2  A: 

getElementsByName and getElementsById are both members of the document object, and aren't part of the HTMLElement prototype. In modern browsers (IE8, Firefox, Chrome, Opera), you can use element.querySelectorAll("*[name='myName']").

Other than that your alternative is to use a library like Sizzle or a framework such as jQuery (which uses Sizzle) to handle selectors.

Andy E
A: 

Basically its is DOM, so every time you search for some element by NAME or ID, it can be started from some reference point.

Say you want some element from Form named as Form1, then you can write

document.Form1.fieldName

which will give you particular field.

But it all should be absolute path. Because if you do have some reference, you can refer to its children.

Jquery anf yUI provides better support for fetching and setting of DOM elements. JQuery eases the things which are hard in javascript.

try www.jquery.com

thanks.

Paarth
@Paarth, I think the whole point is that you can only start from the root by using document.getElementsByTagName. You can't replace document with a node somewhere in the middle of the DOM.
teehoo