6

My question is rather elementary, but I do not understand why, in the following code, on button click only button dissapears, instead of the whole div:

<script>
    function remove(id) {
        //get the element node
        element = document.getElementById(id);

        //remove the element from the document
        document.removeChild(element);
    }
</script>

<div id="intro" class="jumbotron">
    <h1>Your Offline Web Dictionary!</h1>
    <p class="lead">

    <div class="controls">
        <input class="span7 " type="text" placeholder=" " name="key">
    </div>
    <div>
        <button class="btn btn-large btn-success" onclick="remove(intro)">
            Dictionary Search
        </button>
    </div>
</div>

JSFiddle

3 Answers 3

4

The problem is that the button element has a remove property so that is called instead of your remove function. And also the string thing.

<button class="btn btn-large btn-success" onclick="window.remove('intro');console.log(this.remove);">
    Search
</button>

http://jsfiddle.net/HMEVd/76/

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

5 Comments

+1 Yes, it is pretty good explanation. Thanks a lot!! I will accept it within few minutes.
Why would the button's remove get called? Is there some implicit with in action? And where is this remove coming from? It's not a specified DOM method.
Well I saw this behaviour a while back and never really questioned it.
This certainly seems to be correct (look in the console): jsfiddle.net/HMEVd/78
Yet another example of why using DOM0 handlers is a Bad Idea(tm). (And the implicit with on the element in the onclick generated function is coming back to me...)
2

Two problems. Firstly, intro should be a string, not an identifier, so use remove('intro') in your onclick.

Second, document.rwmoveChild is incorrect. removeChild should be called on the parent of the element you are removing. It is common to use:

element.parentNode.removeChild(element);

1 Comment

All quite correct, but it doesn't answer the question the OP asked: Why is the button, not the div, disappearing?
1

intro should be sent to the function as a string rather than a variable, i.e, 'intro'

Also, you must rename your function, for example, removeById instead of remove. Then it works perfectly.

The function remove actually does something completely different. (Your function is not even invoked when it is named remove as you can see by putting an alert message into it.)

function removeById(id) {
        //get the element node
        element = document.getElementById(id);

        //remove the element from the document
        document.removeChild(element);
}

...
<button class="btn btn-large btn-success" onclick="removeById('intro')">

1 Comment

This doesn't explain the symptom the OP quoted: "on button click only button dissapears, instead of the whole div"

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.