3

I'm trying to remove a div in Javascript but 'its not working. I get no error in my console yet the function does get called.

I don't understand what I have done wrong, so I'm hoping someone can explain. This is how it works:

function menu_load(type){
  document.getElementById(type).onclick = function(){ menu_unload(type); }
  var width = 100;
  var height = 100;
  var d = document.createElement('div');
  d.id = 'menu';
  d.className = 'menu';
  d.style.width = width + 'px';
  d.style.height = height + 'px';
  document.getElementById('G').appendChild(d);
}

function menu_unload(type){
  alert('test'); //this displays
  var div = document.getElementById("menu");
  div.parentNode.removeChild(div); // doesn't remove the div
  document.getElementById(type).onclick = menu_load(type);
}

window.onload = function(){
  menu_load('test');
}

Is there any mistake here that I have missed? I just can't work out the problem.

16
  • you say "the function" does get called. Do you mean menu_unload or just menu_load? Commented Dec 16, 2012 at 2:26
  • both, it loads the div when I append and my alert displays for menu_unload with the onclick Commented Dec 16, 2012 at 2:27
  • 1
    put your alert call at the end of menu_unload and see if it still gets called Commented Dec 16, 2012 at 2:29
  • I created a jsfiddle with a smaller test case, and it works... maybe it'll help. jsfiddle.net/KcEKL Commented Dec 16, 2012 at 2:30
  • 1
    You should definitely use jQuery. It's really great and does all things. </sarcasm> Commented Dec 16, 2012 at 2:41

1 Answer 1

5

Your code works for me if I correct the following line:

document.getElementById(type).onclick = menu_load(type);

Which incorrectly calls menu_load() and tries to assign the result to .onclick. It should be like you did in the other function

document.getElementById(type).onclick = function() { menu_load(type); };

Demo: http://jsfiddle.net/MCZza/

To be honest I don't know why this fixes it, since your code wasn't actually a syntax error, but because it called menu_load() it recreated the div just removed. and the .removeChild() line should happen first, but anyway...

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

6 Comments

+1, but @ "I don't know why this fixes it", it is because calling menu_load is creating a new <div> which is exactly the same, so you can't tell the difference by eye.
Ah, thanks @PaulS. - I didn't think it through even though I said "[this] calls menu_load()". I'll update my answer.
@PaulS. - thank you, man, you nailed it. You should put it as an answer.
Ah so it was doing it but immediately re-creating it ?
@Igor as much as I like reputation I don't think a new answer is warranted as this one covers the solution adequately.
|

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.