0

How to write code in JavaScript or jQuery, so call the click event after pressing the button?

HTML:

<form action="#" method="get">

    <button type="submit">Turn to the dark side</button>
</form>
halfer
  • 19,824
  • 17
  • 99
  • 186
J Script
  • 3
  • 3

7 Answers7

1

In Javascript you can write the code in following

Turn to the dark side
    <script>
     function clickMe(){
     alert("Hello World");
    }
    </script>
0

Not really sure what you need but clicking on the button below fires the alert. Put any function (event) in there.

https://jsfiddle.net/9p70hwyL/

<button type="submit" onclick="alert('The event')">Turn to the dark side</button>
Gezzasa
  • 1,443
  • 8
  • 17
0

You can do this :

<button id="target" onclick="myFunction()">Click me</button>

Or with jquery :

$( "#target" ).click(function() {
  alert( "Handler for .click() called." );
});
0

Here is what you need in JavaScript.

function submitForm(){
document.body.style.backgroundColor = "red";
}
#bg{
  width: 200px;
  height: 200px;
  background: green;
}
<div id="bg" ></div> 

<form action="#" method="get">
    <button onclick="submitForm()" type="button">Turn to the dark side</button>
</form>
Harish Soni
  • 1,796
  • 12
  • 27
0

In Javascript, you can write the code in following two ways

<button type="submit" click="alert('Hi');">Turn to the dark side</button>

OR

    <button type="submit" click="buttonClicked();">Turn to the dark side</button>
function buttonClicked() {
   alert('Hi');
}

In Jquery, you can call event by class name (css class) and id.

<button type="submit" id="btn">Turn to the dark side</button>

$("#btn").click({
   alert('Hi');
});

OR

<button type="submit" class="btnClass">Turn to the dark side</button>

$(".btnClass").click({
   alert('Hi');
});
Amit Prajapati
  • 1,190
  • 2
  • 9
  • 13
0

Theres the html onClick() method.

For example

<button onClick="return sampleFunction()">Click me!!!</button>

In JavaScript

function sampleFunction()
{ 
    alert("abc"); 
    return false;
}

This return false will stop the button's default action, for example like form posting. If you return true, it'll continue.

Buzzzzzzz
  • 1,074
  • 13
  • 17
0

I'm sorry but it did not help me. Look at this:

    <form action="http://youtube.com" method="get">
  <ul>
    <li>
      <input id="checkbox_vader" type="checkbox" name="character" value="dark_side">
      <label for="checkbox_vader" data-sentence="There is no escape from the Dark Side.">Vader</label>
    </li>
  </ul>
  <button type="submit">Turn to the dark side</button>
</form>

I would like to do if-else in javascript/jquery. Please, can u told me, how to start writing code in js / jq?

J Script
  • 3
  • 3