...

View Full Version : add/remove div's question



Touch
02-04-2009, 05:28 AM
Hey everyone,

I am new to JS and this forum

Was hoping i could get some help using some code i am trying to get working on my site. I am using Justin Diaz's add and remove form elements code. I have it working but i want to get it working on the same page with twice and i was just wondering what i have to change to get it working

here is the code so anyone can see



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);
};
}
}()
};
Event.add(window, 'load', function() {
var i = 0;
Event.add('add-element', 'click', function() {
var el = document.createElement('p');
el.innerHTML = 'Remove This Element (' + ++i + ')';
Dom.add(el, 'content');
Event.add(el, 'click', function(e) {
Dom.remove(this);
});
});
});





And html



<div id="doc">
<h1>Add &amp; Remove Elements with JavaScript</h1>
<p id="add-element">Add Elements</p>
<div id="content"></div>
</div>


Any help would be awesome....thanks!!

Touch
02-04-2009, 07:20 AM
Sorry everyone. i figured out what i needed to do.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum