cabbage dude
cabbage dude

Reputation: 71

My button for a script doesn't work and only shows the code

I am using html to try to create an calculator I am using HTML for the buttons to run javascript.

<form method="get" action="a.js">
    <button  onclick="a.js">addition</button>
</form>

Upvotes: 1

Views: 335

Answers (2)

Lu&#237;s Ramalho
Lu&#237;s Ramalho

Reputation: 10218

Here's a basic example to get you started:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Button Click</title>
  </head>

  <body>
    <form method="get" action="a.js">
      <button onclick="handleClick()">addition</button>
    </form>
    <script type="text/javascript">
      function handleClick() {
        console.log("clicked.");
      }
    </script>
  </body>
</html>

function handleClick() {
  console.log("clicked.");
}
<form method="get" action="a.js">
  <button onclick="handleClick()">addition</button>
</form>

Upvotes: 1

clota974
clota974

Reputation: 505

Indeed, when you click the submit button, it opens the a.js file instead of doing what you want.

If you want to send a request (as you provided a form), you need a server to process the data.

In your case, you may want to :

1- Import your script with the script tag

2- Replace your form attribute with action="#"

3- Call the function with onclick='myFunction() in your button tag (onclick lowercase according to w3schools)

Upvotes: 0

Related Questions