Javascript Code
function myFunction {
var response = "Hi";
alert(response);
}
HTML code
<button onclick="myFunction">Click Me</button>
Instead of working the console es up with the error above. The script was placed after the HTML code in the webpage. Any help will be greatly appreciated.
Javascript Code
function myFunction {
var response = "Hi";
alert(response);
}
HTML code
<button onclick="myFunction">Click Me</button>
Instead of working the console es up with the error above. The script was placed after the HTML code in the webpage. Any help will be greatly appreciated.
Share Improve this question edited Feb 16, 2017 at 9:42 Oscar Peace asked Feb 15, 2017 at 19:36 Oscar PeaceOscar Peace 991 gold badge2 silver badges11 bronze badges 2-
First of all.. you have invalid HTML there. You need to close the
onclick
. Secondly, this is not enough information to help you. Where did you define the function? Was the button created before or after that function? – putvande Commented Feb 15, 2017 at 19:39 -
2
Missing closing
"
on theonclick
function. Missing()
at the end of the function call. Missing()
in the function declaration. I'd remend a basic JavaScript tutorial – tymeJV Commented Feb 15, 2017 at 19:39
2 Answers
Reset to default 3Your syntax is a bit off...
JavaScript Code
function myFunction() {
var response = "Hi";
alert(response);
}
HTML Code
<button onclick="myFunction()">Click Me</button>
1. You are missing the brace()
after the function name. So function myFunction {
should be function myFunction() {
2. In the HTML, you are missing the closing quotation marks " and also need to invoke the method on the onclick
attribute.
onclick="myFunction()"
function myFunction() {
alert("I've been clicked!!");
}
<button onclick="myFunction()">Click Me</button>