469,611 Members | 1,711 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,611 developers. It's quick & easy.

Adding methods to Node or Element objects?

I'm wondering if it's possible to add a method to the Node and/or
Element objects (so they'd subsequently be available to any
node/element). I'd assume you could just do something like:

n.myNewMethod = function myNewMethod;

where n is any Node -- but the fact that Nodes and Elements are
explicitly definied as interfaces rather than actual objects in the DOM
standard makes me wary. Javascript/ECMAscript, iirc, however, uses a
prototype object-oriented model that doesn't have interfaces, right?

Thanks,
Weston

~==~
http://weston.canncentral.org/
Taking Pictures During Dreams
weston8[at]cann8central.org
(remove eights to email me)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
2 1752
Weston C <west8on[at]cann8central.RemoveEights.org> writes:
I'm wondering if it's possible to add a method to the Node and/or
Element objects (so they'd subsequently be available to any
node/element). I'd assume you could just do something like:
Not generally.
In Mozilla, you can use
Node.prototype.myNewMethod = function (...){...};
Other browsers don't have the constructor function for nodes available.
Javascript/ECMAscript, iirc, however, uses a prototype
object-oriented model that doesn't have interfaces, right?


Yes. There is no requirement in the W3C DOM that the ECMAScript binding
makes constructors available. You have the factory methods on the document
object if you need to create elements.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Weston C wrote:
I'm wondering if it's possible to add a method to the Node and/or
Element objects (so they'd subsequently be available to any
node/element). I'd assume you could just do something like:

n.myNewMethod = function myNewMethod;

where n is any Node -- but the fact that Nodes and Elements are
explicitly definied as interfaces rather than actual objects in the DOM
standard makes me wary. Javascript/ECMAscript, iirc, however, uses a
prototype object-oriented model that doesn't have interfaces, right?

Thanks,
Weston


Sure, the following works in Gecko based browsers (Mozilla Firebird,
Mozilla, Netscape 7.x, Camino):

<div id="blah"></div>
<div id="bleh"></div>
<script type="text/javascript">
HTMLDivElement.prototype.myId = function() {
alert(this.id);
}
</script>
<button onclick="document.getElementById('blah').myId();"> blah</button>
<button onclick="document.getElementById('bleh').myId();"> bleh</button>

I figured out how to this here: <url:
http://www.mozilla.org/docs/dom/mozilla/protodoc.html />

For most other browsers you'll just have to assign the method to all the
elements of that type at run-time:

<div id="blah"></div>
<div id="bleh"></div>
<script type="text/javascript">
var divs = document.getElementsByTagName('div');
if (divs != null) {
for (var i = 0; i < divs.length; i++) {
divs[i].myId = myId;
}
}
function myId() {
alert(this.id);
}
</script>
<button onclick="document.getElementById('blah').myId();"> blah</button>
<button onclick="document.getElementById('bleh').myId();"> bleh</button>

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Joe Kelsey | last post: by
2 posts views Thread by manohar.shankar | last post: by
6 posts views Thread by Ron Brennan | last post: by
9 posts views Thread by Michelle | last post: by
2 posts views Thread by Maersa | last post: by
5 posts views Thread by Water Cooler v2 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.