share
Stack Overflowclose icon doesn't appear
[0] [1] Evanescence
[2011-06-02 05:05:17]
[ javascript popup ]
[ https://stackoverflow.com/questions/6210633/close-icon-doesnt-appear ]

I was trying to add a text popup box on my page, and this code helped me but I need to add a close icon (which is an image in my code)..

but it doesn't work :/

here is my code:

function show_hide_box(an, width, height, borderStyle) {

if (navigator.userAgent.indexOf("MSIE") != -1) {
    var browserIsIE = true;
} else { browserIsIE = false; }


var href = an.href;
var boxdiv = document.getElementById(href);

if (boxdiv != null) {
if (boxdiv.style.display=='none') {
  move_box(an, boxdiv);
  boxdiv.style.display='block';
} else
  boxdiv.style.display='none';
return false;
}

boxdiv = document.createElement('div');
boxdiv.setAttribute('id', href);
boxdiv.style.display = 'block';
boxdiv.style.position = 'absolute';
boxdiv.style.width = width + 'px';
boxdiv.style.height = height + 'px';
boxdiv.style.border = borderStyle;
boxdiv.style.backgroundColor = '#FFF';

var inClosebox = document.createElement("div");
inClosebox.setAttribute('id', 'Close');
inClosebox.style.position = 'absolute';

if (browserIsIE) {
  inClosebox.style.left = '-1px';
  inClosebox.style.top = '0px';
} else {
  inClosebox.style.left = '-15px';
  inClosebox.style.top = '-15px';
}

inClosebox.style.visibility = 'hidden';

var inImage2 = document.createElement("img");
inImage2.onclick = function () { this.document.close(); };
inImage2.setAttribute('src', '../../Images/closebox.png');
inImage2.setAttribute('width', '30');
inImage2.setAttribute('height', '30');
inImage2.setAttribute('border', '0');
inImage2.style.cursor = 'pointer';
inClosebox.appendChild(inImage2);


var contents = document.createElement('iframe');
contents.scrolling = 'yes';
contents.frameBorder = '0';
contents.style.width = width + 'px';
contents.style.height = height + 'px';
contents.src = href;

boxdiv.appendChild(contents);
boxdiv.appendChild(inClosebox);
document.body.appendChild(boxdiv);
move_box(an, boxdiv);

return false;
}

can any help me please?

if you are using firefox, use/Download Firebug and try positioning the closebox to where you want and you can use these values in your css - Ibu
try to set the style in a class and simply give your elements classNames - Ibu
I do.. and do use firebug. but the code closebox doesn't appear at all. I'm wondring if there is something wrong in my code? - Evanescence
Just like naveen suggested check to see if the image is in the correct path - Ibu
if I want to retrive a specific part from the page can I do that? I mean at this part: var href = an.href; var boxdiv = document.getElementById(href); can I specify an ID of a tag to retrive it? like : var href = an.href; var boxdiv = document.getElementById(href).getElementById("Show This Part!"); ?? - Evanescence
[+1] [2011-06-02 05:12:54] codeandcloud [ACCEPTED]

That should mean that the path of src is wrong. ie, ../../Images/closebox.png

Add this to your code and see whether this works

inImage2.setAttribute('alt', 'Close');

Even if this doesn't work, it shows you that something else is wrong with the code.

Its a very good practice to add alt attribute to img tag always.

Update:

I just saw this inClosebox.style.visibility = 'hidden'; You are appending img to that and so how are you gonna possibly make it visible when the parent is hidden?
Beats me. Or do you have extra code? If no, please remove that line and try.


its in the right path, and yes, I forgot to set the visibility! thanks naveen and Ibu.. but unfortunately, inImage2.onclick = function () { this.document.close(); }; does not work right, what shall I use to make the box close after clicking the icon? - Evanescence
it works now, I did use: document.body.removeChild(boxdiv); thnx everybody! - Evanescence
I have one more question :$ I've wrote it in the comment section above. - Evanescence
could you possibly post it as a separate question. would like a little more elaboration. - codeandcloud
sure, I did: stackoverflow.com/questions/6212596/… - Evanescence
1