Noam Tan
Noam Tan

Reputation: 1

Can't call function with onclick attribute

Why doesn't this work?

html :

<html>
 <head>
  <title>test</title>
 </head>
 <body>
  <button onclick="test21()">test</button>
 </body>
</html>

javascript:

function test21(){
    console.log("yey");
}

Upvotes: 0

Views: 2177

Answers (3)

prophet1906
prophet1906

Reputation: 123

You have not included the script in html file.

<html>
 <head>
  <title>test</title>
   <script>
     function test21(){
      console.log("yey");
      }
   </script>
 </head>
 <body>
  <button onclick="test21()">test</button>
 </body>
</html>

Above code snippet will run fine.
Use inspector (ctrl+shift+I) to launch console and click on the button to get results.

Upvotes: 0

Brandon
Brandon

Reputation: 3266

That works fine. Here's a JSBin to prove it.

http://jsbin.com/luxesazibi/edit?html,output

More than likely you haven't loaded your script in your HTML. Where are you putting your javascript?

Upvotes: 1

user7020195
user7020195

Reputation:

You can create a single function that calls both of those, and then use it in the event.

function myFunction(){
    pay();
    cls();
}

And then, for the button:

<input id="btn" type="button" value="click" onclick="myFunction();"/>

Upvotes: 0

Related Questions