0

I am trying to make HTML print out "Germany" when you click the button. But it is not working correctly. Is there something wrong in my code?

HTML

<input type="button" value="click!" onclick="shuffle();"/>
<div id="output"></div>

Javascript

(function(){
    window.onload = function(){
        alert("welcome to my trip record");
    }
})();

var shuffle = function(){
    var target = document.getElementById("output");
    target.innerHTML = "Germany";
}
1
  • You have Doom event Listener. Commented Apr 14, 2017 at 1:25

4 Answers 4

1

Remove the ; from your onclick

<input type="button" value="click!" onclick="shuffle()"/>
Sign up to request clarification or add additional context in comments.

Comments

1

Try to use addEventListener for make event on button click

document.getElementById("myBtn").addEventListener("click", function(){
    var target = document.getElementById("output");
    target.innerHTML = "Germany";
});

here is the working jsfiddle:https://jsfiddle.net/jxjpzvvz/

Comments

0

You made two mistakes First mistake <input /> this is nothing. Second most common "function ();", the ";" Is wrong

Your corrected code

(function(){
    window.onload = function(){
        alert("welcome to my trip record");
    }
})();

var shuffle = function(){
    var target = document.getElementById("output");
    target.innerHTML = "Germany";
}
<input type="button" value="click!" onclick="shuffle()"/>
<div id="output"></div>

I like to use DOM EventListener

window.onload = function(){
    alert("welcome to my trip record");
}
document.getElementById("myBtn").addEventListener("click", function_t);
function function_t(){
var target = document.getElementById("output");
target.innerHTML = "Germany";
}
 <input type="button" value="click!" id = "myBtn" >
 <div id="output"></div>

Comments

0
<button id='btn_click' type="button">Click me!</button>
<div id="output"></div>

use button element for buttons, not input type button. we are not in the time of html 4.0 ;)

(function(){
    window.onload = function(){
        alert("welcome to my trip record");
    }
})();

var shuffle = function(event){
    var target = document.getElementById("output");
    target.innerHTML = "Germany";
}

document.getElementById('btn_click').addEventListener('click', shuffle);

i'm fan of no js in the html tags directly, so i add a listener to the element

Comments

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.