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>
7
  • 1
    call what event, the submit event in a form or? Commented May 9, 2018 at 11:19
  • yes, this button is in a form Commented May 9, 2018 at 11:19
  • it will be called automatically Commented May 9, 2018 at 11:20
  • halfter, i want to call click Commented May 9, 2018 at 11:21
  • Look here. Commented May 9, 2018 at 11:22

7 Answers 7

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>
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this :

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

Or with jquery :

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

Comments

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');
});

Comments

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>

1 Comment

Great, although the function is now misnamed :-)
0

In Javascript you can write the code in following

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

Comments

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.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.