0

I need to empty an h2 element when a button is clicked. I do this in part of my code already and it works fine, but when I try to empty this part of my code it doesn't work. I can't figure out why.

JSFiddle link here

 $('#help').click(function() {
    $('#intro').empty();
    $('#intro').text("News:");
    $('#help').hide();
    $('#noHelp').hide();
    $('#news').text("Battle system created");
   });

This part is breaking it and I don't know why

var fly = new Monster("fly", 1, 1, 5, 2, 1, 1);
var mouse = new Monster("mouse", 2, 3, 10, 2, 1, 2);
var rat = new Monster("rat", 4, 5, 20, 4, 2, 2);
var rabidChihuahua = new Monster("chihuahua", 6, 8, 35, 6, 1, 4);
var bulldog = new Monster("bulldog", 10, 14, 60, 10, 4, 1);
var wolf = new Monster("Wolf", 15, 18, 65, 12, 3, 6);
var vampie = new Monster("Vampire", 20, 23, 100, 12, 5, 4);
var werewolf = new Monster("Werewolf", 25, 29, 100, 14, 3, 9);
var giantSlime = new Monster("Giant Slime", 31, 38, 200, 7, 15, 1);
var babyDragon = new Monster("Baby Dragon", 39, 50, 150, 16, 9, 5);
var orc = new Monster("Orc", 50, 64, 220, 10, 12, 4);
var succubi = new Monster("Succubi", 61, 80, 190, 21, 8, 12);
var elderDragon = new Monster("Elder Dragon", 75, 100, 300, 21, 15, 8);
var sanaan = new Monster("Sanaan", 150, 500, 500, 55, 45, 30);
5
  • are you creating the button html dynamically? using ajax or something? Commented Mar 17, 2016 at 18:39
  • I make the button in the html. Commented Mar 17, 2016 at 18:42
  • Check your console, you have a javascript error. Commented Mar 17, 2016 at 18:47
  • could you show me where? I rand this through the console and jshint and came up with no errors Commented Mar 17, 2016 at 18:47
  • Uncaught TypeError: Cannot read property 'appendChild' of null on monsterList.appendChild(opt);. Looks like monsterList is null. Commented Mar 17, 2016 at 18:51

1 Answer 1

2

I've updated your fiddle :

see here

I've changed this line to test the opt var :

if(opt.length > 0){
    monsterList.appendChild(opt); // appending option to select element
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you that fixes it. Could you explain why that part would be breaking my button code when it didn't break any of the other code under it?
When a JS error is encountered, it stops execution of the code. So when you click on the button with a JS error in your code which is below, it doesn't works because your function is below the other one which has an error. If you put it above, it works.

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.