what's the difference between getAttribute and getAttributeNode?

483 views
Skip to first unread message

bobo

unread,
Dec 6, 2006, 10:00:06 PM12/6/06
to greasemonkey-users
Confusing ....
one more question here ...
can I take "onclick" as a attribute of some element so that I can use
removeAttribue to remove the event handler?

chris feldmann

unread,
Dec 7, 2006, 11:01:05 PM12/7/06
to greasemon...@googlegroups.com
A quick test reveals the answer is yes:
Document:

<a id="a" onclick='alert("hi");'>hi</a>

click, get "hi". Open a shell and go:

a = document.getElementById("a")
a.setAttribute("onclick", function(){return false;})

Click. Nothing.

chris feldmann

unread,
Dec 7, 2006, 11:03:06 PM12/7/06
to greasemon...@googlegroups.com
Ooops, not quite what you asked. Well, this works too:

a = document.getElementById("a")

a.removeAttribute("onclick")

bobo

unread,
Dec 8, 2006, 12:47:14 AM12/8/06
to greasemonkey-users
Thanks chris.
So ... the question remain is:

what's the difference between getAttribute and getAttributeNode

Dave Land

unread,
Dec 8, 2006, 1:56:30 AM12/8/06
to greasemon...@googlegroups.com

If I RTFD (http://developer.mozilla.org/en/docs/DOM:element) properly,
getAttribute returns the *value* of the attribute and getAttributeNode
returns an Attr node...

HTML:

<a id="a" onclick='alert("hi");'>hi</a>

Script:

a = document.getElementById("a");

a.getAttribute("onclick");
--> alert("hi")

a.getAttributeNode("onclick");
--> Attr:
localName = onclick
nodeValue = alert("hi")
nodeName = onclick

Try it in FireBug!

Dave

bobo

unread,
Dec 10, 2006, 10:08:56 PM12/10/06
to greasemonkey-users
Thanks

Arvid Jakobsson

unread,
Dec 12, 2006, 2:27:16 AM12/12/06
to greasemon...@googlegroups.com
Recently I tried to remove the onkeydown eventlistener on a page's body.
Seems like
document.body.removeAttribute('onkeydown');
didn't work at all, but
document.body.setAttribute('onkeydown', function(){return false;});
worked just fine. Strange.

On 12/11/06, bobo <bobo...@gmail.com> wrote:

Thanks





Reply all
Reply to author
Forward
0 new messages