ronald Posted February 20, 2015 Share Posted February 20, 2015 hello, i tried to build a very simple function, and i failed allready. dissapointing. I can not find where i went wrong. plz help me. button 1: it should change the wordt "haai" to "yoyo" and then to "hallo". it does change haai to yoyo, but not beyond. button 2: it should change color (it does, so works fine). regards, ronald <script> function hello() { var x = document.getElementById("hey"); if(x = "haai"){ var y = "yoyo"; }else{ var y = "hallo"; } document.getElementById("hey").innerHTML = y; } function red(){ document.getElementById("hey").style.color = "red"; } </script> <button onClick="hello()">click me</button> <button onClick="red()">click me</button> <p id="hey">haai</p> Link to comment Share on other sites More sharing options...
Ingolme Posted February 20, 2015 Share Posted February 20, 2015 You'll first need to get the innerHTML property of the element. In your if() statement you need to use comparison operators, a single = is an assignment operator which is something different. Link to comment Share on other sites More sharing options...
davej Posted February 20, 2015 Share Posted February 20, 2015 <!DOCTYPE html><html lang="nl"><head><meta charset="utf-8"><title>tab</title></head><body><button onClick="hello()">click me</button><button onClick="red()">click me</button><p id="hey">haai</p><script> function hello() { var x = document.getElementById("hey").innerHTML; if(x == "haai"){ var y = "yoyo"; }else{ var y = "hallo"; } document.getElementById("hey").innerHTML = y; } function red(){ document.getElementById("hey").style.color = "red"; }</script></body></html> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now