Posted By

LeeRJohnson on 02/12/08


Tagged

get event elements DOM remove add


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

SpinZ
rickygri


Add and Remove Elements with JavaScript (reprise)


 / Published in: HTML
 

URL: http://www.dustindiaz.com/add-remove-elements-reprise/

  1. var Dom = { get: function(el) { if (typeof el === 'string') { return document.getElementById(el); } else { return el; } }, add: function(el, dest) { var el = this.get(el); var dest = this.get(dest); dest.appendChild(el); }, remove: function(el) { var el = this.get(el); el.parentNode.removeChild(el); } }; var Event = { add: function() { if (window.addEventListener) { return function(el, type, fn) { Dom.get(el).addEventListener(type, fn, false); }; } else if (window.attachEvent) { return function(el, type, fn) { var f = function() { fn.call(Dom.get(el), window.event); }; Dom.get(el).attachEvent('on' + type, f); }; } }() };

Report this snippet  

You need to login to post a comment.