0

Some objects in javascript can be appended a method to it. For example we can have Array.prototype.pop = function() {...}

So what I wanna ask is that can we append a method to DOM elements?

Suppose I have the following

var x = document.getElementById("hello");
x.toggle();

How can I append a method called toggle to DOM elements just like what we do in jquery??

Thank you so much.

sc1013
  • 1,068
  • 2
  • 16
  • 24
  • possible duplicate of [In Javascript, can you extend the DOM?](http://stackoverflow.com/questions/779880/in-javascript-can-you-extend-the-dom) – Felix Kling May 03 '12 at 10:46
  • Also related: [Is there really no way to expose the prototype of a html element in IE (<8)?](http://stackoverflow.com/questions/592815/is-there-really-no-way-to-expose-the-prototype-of-a-html-element-in-ie-8) – Felix Kling May 03 '12 at 10:47

1 Answers1

1

In concept, you can append functions since DOM elements are Objects after all. In practice, however, you should avoid doing it. Circular references is one of the issues that is related to binding objects to DOM elements which can cause memory "leakage"

Joseph
  • 117,725
  • 30
  • 181
  • 234