0

I have an HTML page with a heading and 2 Divs, I then have a javascript where I want the heading to change if the user selects one off the Divs.

https://jsfiddle.net/L27xqgfs/1/

var happyMood = getElementById("happy");
var sadMood = getElementById("sad");


happyMood.onclick = function () {
    var mainHeading = getElementById("heading");
    mainHeading.innerHTML = "You have selected ";

};

sadMood.onclick = function () {
    var mainHeading = getElementById("heading");
    mainHeading.innerHTML = "You have selected " + sadMood;
};

Please can someone advise where I've gone wrong?

Thanks in advance.

4
  • 4
    getElementById() is not a global function, try document.getElementById(). Start there. Commented Sep 27, 2015 at 8:27
  • document.getElementById not getElementById! Commented Sep 27, 2015 at 8:27
  • Fiddle Commented Sep 27, 2015 at 8:30
  • Also after you make the change suggested in other comments, if you try to concatenate sadMood object with a string you won't get the result you would expect. Instead use its ID to get requested result. Commented Sep 27, 2015 at 8:33

2 Answers 2

2

if you call getElementById without any object,then you call window. getElementById, but you really should call document.getElementById. And you should import js file into html(in you local test). I have created another jsfiddle which is correct.

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

Comments

0
var happyMood = document.getElementById("happy");
var sadMood = document.getElementById("sad");


happyMood.onclick = function () {
   var mainHeading =     document.getElementById("heading");
    mainHeading.innerHTML = "You have selected ";

};

sadMood.onclick = function () {
var mainHeading =    document.getElementById("heading");
    mainHeading.innerHTML = "You have selected " +   sadMood;
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.