最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript - Uncaught ReferenceError: myFunction is not defined - Stack Overflow

programmeradmin2浏览0评论

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 the onclick 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
Add a ment  | 

2 Answers 2

Reset to default 3

Your 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>

发布评论

评论列表(0)

  1. 暂无评论