...

View Full Version : Moz dynamic attributes



Garadon
03-17-2004, 09:44 PM
in IE I can do this:

HTML
<a attribute="somevalue">
js
alert(document.getElementsByTagName('A').attribute);


why can't I do that in moz/firefox?

sad69
03-17-2004, 11:08 PM
Because IE doesn't follow the standard!

The correct way to do this (and this should be in the DOM forum) is as follows:


alert(document.getElementsByTagName('A').item(0).attributes.getNamedItem("attribute").nodeValue);


At least I'm pretty sure that's the way...

For more information on this kind of stuff, refer to my favourite API:
http://krook.org/jsdom/Node.html

Hope that helps,
Sadiq.

brothercake
03-18-2004, 03:13 AM
Originally posted by Garadon
alert(document.getElementsByTagName('A').attribute);
getElementsByTagName is a collection - you have to specify the element's index in the collection. Better also to search in lower case:


alert(document.getElementsByTagName('a')[0].attribute);

liorean
03-18-2004, 11:09 AM
sad69: Why do it more complicated than necessary?
document.getElementsByTagName('a').item(0).getAttribute('attribute')

sad69
03-18-2004, 05:30 PM
Because I didn't know about that one! I don't think I've really ever looked at the Element class before...

Good call though, I'll use that from now on!

Cheers,
Sadiq.

Garadon
03-18-2004, 06:31 PM
ty :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum