2

i have a chat window which has a close button(input type='image') and onclick i want to remove the chat window from the parent node and i use this code

closeBtn.setAttribute("onclick", "return closeContainer(cc" + friendId + ");");

please note that this button is created through javascipt and the cc223423432 (say) will be the id of the chat window

here is my code to remove it

function closeContainer(ccId) {
    var x = ccId;
    x.parentNode.removeChild(x);
    //..........
}

now in IE8 and Chrome it finds the passed argument as HTMLDIV and works fine but in firefox it give an error cc223423432 is undefined any idea why???

i know i can always do a document.getElementByID and then remove it but please if there is anything i am missing please tell

1
  • Thanks guys, i needed to know that i wasnt missing anything....i will change the code as needed Commented Oct 5, 2009 at 7:00

5 Answers 5

4

closeBtn.setAttribute("onclick", "return closeContainer(cc" + friendId + ");");

Don't use setAttribute to set event handlers... actually don't use setAttribute on an HTML document at all. There are bugs in IE6-7 affecting many attributes, including event handlers. Always use the ‘DOM Level 2 HTML’ direct attribute properties instead; they're reliable and make your code easier to read.

Lose the attempt to create a function from a string (this is almost always the wrong thing), and just write:

closeBtn.onclick= function() {
    return closeContainer(document.getElementById('cc'+friendId));
};

or even just put the closeContainer functionality inline:

closeBtn.onclick= function() {
    var el= document.getElementById('cc'+friendId);
    el.parentNode.removeChild(el);
    return false;
};

in firefox it give an error cc223423432 is undefined any idea why???

Because IE makes every element with an id (or in some cases name) a global variable (a property of window). So in IE you can get away with just saying cc223423432 and getting a reference to your object.

This is a really bad thing to rely on, though. As well as not existing in other browsers, it clutters up the global namespace with crap and will go wrong as soon as you do actually have a variable with the same name as an id on your page.

Use getElementById to get a reference to an id'd node instead, as above. This works everywhere and is unambiguous.

Sign up to request clarification or add additional context in comments.

Comments

3

Since the parameter is not enclosed in quotes Firefox considers this as a variable with name cc223423432 which is not defined. Thats why it generates the error.

Better to pass this as a string and then use

document.getElementById ( parameter );

to get the object.

Comments

2

You need to pass that id as a string:

closeBtn.setAttribute("onclick", "return closeContainer('cc" + friendId + "');");

Then:

function closeContainer(ccId) {
    var x = document.getElementById(ccId);
    x.parentNode.removeChild(x);
    //..........
}

Comments

1

jquery will work in all of those browsers. I would do the following:

closeBtn.click(function(){
    ('#cc' + friendId ).remove();
});

However, if you don't want the ramp-up time of learning the jquery api, it looks like you need to pass your id parameter as a string, otherwise firefox considers this a variable name (notice the additional single quotes inside the closeContainer call:

closeBtn.setAttribute("onclick", "return closeContainer('cc" + friendId + "');");

2 Comments

+1, I'm new to jQuery but so far I love it -- everything is so simple and works without all the cross-browser headaches (from what I've experienced).
As much that could be said of adopting any JavaScript framework, this kind of misses the point - that puts the OP no closer to a goal (unless it was how to do it in a framework, which is not always an option)
1

Or you could do the following

function handler(elementRef) {
  console.log(elementRef);
}
<button onclick="handler(this)">Click Me!</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.