473,513 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1873
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
17183
by: Joe Kelsey | last post by:
When you use addEventListener (or addEvent in IE) to call an object method, does it call it with the correct this parameter? The ECMAScript reference has a lot to say about the caller using...
2
2295
by: manohar.shankar | last post by:
Hi, I have been searching on this topic for quite sometime and didnt get any answer. Is there a way I can extend/add methods/properties to a C# class during runtime. eg., I have class:...
6
28390
by: Ron Brennan | last post by:
I want if possible to use something like: element.getElementsByTagName('text') but that doesn't work. Is there another value for the parameter, or is it not possible with getElementsByTagName?...
9
39749
by: Michelle | last post by:
I have a div that is initially empty. Clicking on a button will add some text boxes and other controls so the user can add additional records. In IE all works fine but in Netscape 7.0 when I add...
2
9283
by: Maersa | last post by:
hi, i'm trying to add "attributes" to some nodes and it works at design time but fails at runtime. XmlDocument doc = new XmlDocument(); XmlElement elem = doc.CreateElement( "root" );...
3
6866
by: Newcomsas | last post by:
Hello, I'm trying to solve a problem with JS textbox array without success. I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new textbox named 'dear' is generated. So, if...
5
4576
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
0
2786
by: theonlydavewilliams | last post by:
Hi there I'm hoping there's an easy answer to a (hopefully) not too long-winded issue... I'm building a C# web client using a proxy wsdl.exe'd from a wsdl file and six schemas, each in a different...
6
2490
by: SQACSharp | last post by:
I'm using the EnumChildWindows API with an EnumChildWndProc callback to populate the treeview. The output will be something similar to spy+ + How can I specify the parent when adding a new node...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.