adding a dynamically created element
I'm trying to dynamically add an input box element directly after an input checkbox, onclick of that checkbox. I am able to create the input box dynamically but since the checkbox I'm trying to add after is not a parent I can't append a child to it. How can I get around this? Which function do I use?
Thanks
Need the Element to be more specific
The code works great...the one from Fang, however, I need it to be more specific. I have several 'input' tags on the page and this allows the text box to show on every one of them. I want it to be on the ones that are only referring to checkboxes. I've tried everything I can think of and can't get it to refer just to checkboxes. Any suggestions?
function addElement() {
var aInput=document.getElementById('myspan').getElementsByTagName('input');Need this to refer to Checkboxes only
for(var i=0; i<aInput.length; i++) {
aInput[i].onclick=new Function('addDelete(this)');
}
}
function addDelete(obj) {
var parentSpan=document.getElementById('myspan');
if(obj.nextSibling.nodeName!='INPUT') { // add
var oInputText=document.createElement('input');
oInputText.setAttribute('type', 'textarea');
parentSpan.insertBefore(oInputText, obj.nextSibling);
}
else { // delete
parentSpan.removeChild(obj.nextSibling);
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
addElement();
});