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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting an attribute to the first child of an element

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
Hallo,

I would like to find an element (by id) in my page, to verify if a child exist, and in this case, to set an attribute to it.

The code I came out with is this:

Code:
window.onload = function()
{
	var hmnode = document.getElementById('HouseMenuNavCurrentItem');

	if (hmnode.childNodes[1])
	{
		var element = hmnode.childNodes[1];
		element.setAttribute('style', 'display:show');
	}
};

But I get an error when I try to set the attribute. Does someone see a mistake? Thanks
 
"style" isn't really an attribute, but more of a collection of attributes. Also, 'show' isn't a valid value anyway.

Use this instead:

Code:
element.style.display = 'block';

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You have to be careful about the counting of childNodes collection which is different across-browsers depending on the default setting of how to treat whitespaces node. As such the script is very fragile. "style", on the face of it, seems to be an attribute: it is not. But I won't repeat what said.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top