Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing childNodes by name/id 1

Status
Not open for further replies.

starsky51

MIS
Mar 11, 2002
91
Hi All,
I need to find a way to reference the childNodes of a form by their id/name.
I have pasted an example of what i am trying to do. I can reference the node by number (eg. document.f1.childNodes[1]) but not by name. document.getElementById("d2") will not work because there is more than one element with the name "d2".
Does anyone have any ideas?
Dave.
Code:
<html>
<script language="javascript">
	function hidef1d2() {
		document.f1.childNodes["d2"].style.display = "none";
	}
</script>
<body>
	<form id="f1" name="f1">
		<div id="d1" name="d1">
			<input id="i1" name="i1" type="text"/><br/>
			<input id="i2" name="i2" type="text"/><br/>
		</div>
		<div id="d2" name="d2">
			<input id="i3" name="i3" type="text"/><br/>
			<input id="i4" name="i4" type="text"/><br/>
		</div>
	</form>
	<form id="f2" name="f2">
		<div id="d1" name="d1">
			<input id="i1" name="i1" type="text"/><br/>
			<input id="i2" name="i2" type="text"/><br/>
		</div>
		<div id="d2" name="d2">
			<input id="i3" name="i3" type="text"/><br/>
			<input id="i4" name="i4" type="text"/><br/>
		</div>
	</form>
	<input type="button" value="hidef1d2" onclick="hidef1d2();"/>
</body>
</html>
 
Elements shouldn't have the same ID.

But if you can get a handle on the ancestor node you're interested in and know the tag name of the element you're after, you can call getElementsByTagName and then loop through the result to find the node you want.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
ah brilliant.
I ended up using
document.f1.getElementsByTagName("*")["d2"]
which does exactly what i'm after.
Thanks for the idea!
Dave.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top