bcatt
06-30-2008, 02:45 AM
I have created a dynamic div, and a link with which to close it, however, I can't get the close link to work. Here's my code:
Link calls function to create div:
<a id="email" href="javascript:newdiv('mail')">email</a>
Browser workaround:
function check(id){
var elid;
if (document.getElementById){
elid = document.getElementById(id);
}
else if (document.all){
elid = document.all[id];
}
else {
elid = document.layers[id];
}
return elid;
}
Function makes sure div does not exist yet, and creates it if it doesn't (div is then styled using an external css script):
function newdiv(unique){
var vis = check(unique);
if (vis == null){
cdiv = document.createElement("div");
cdiv.id = unique;
document.body.appendChild(cdiv);
container = check(unique);
aclose = document.createElement("a");
aclose.id = unique + "close";
aclose.href = "javascript:closediv("+unique+")";
aclose.innerHTML = "X";
container.appendChild(aclose);
}
}
Up to this point, everything works great, now I try to add a close button. I have tried:
function closediv(id){
var x = check(id);
x.parentNode.removeChild(x);
}
and:
function closediv(id){
var x = check(id);
this.parentNode.parentNode.removeChild(x);
}
and a bunch of other variations that I can't remember now. None work. Please help.
Link calls function to create div:
<a id="email" href="javascript:newdiv('mail')">email</a>
Browser workaround:
function check(id){
var elid;
if (document.getElementById){
elid = document.getElementById(id);
}
else if (document.all){
elid = document.all[id];
}
else {
elid = document.layers[id];
}
return elid;
}
Function makes sure div does not exist yet, and creates it if it doesn't (div is then styled using an external css script):
function newdiv(unique){
var vis = check(unique);
if (vis == null){
cdiv = document.createElement("div");
cdiv.id = unique;
document.body.appendChild(cdiv);
container = check(unique);
aclose = document.createElement("a");
aclose.id = unique + "close";
aclose.href = "javascript:closediv("+unique+")";
aclose.innerHTML = "X";
container.appendChild(aclose);
}
}
Up to this point, everything works great, now I try to add a close button. I have tried:
function closediv(id){
var x = check(id);
x.parentNode.removeChild(x);
}
and:
function closediv(id){
var x = check(id);
this.parentNode.parentNode.removeChild(x);
}
and a bunch of other variations that I can't remember now. None work. Please help.