Reputation: 4051
I am trying to trigger a function when the form submit button is clicked. I tried both with click and submit methods but it is not triggered. Any advice would be greately appreciated.
<form id="my_form" action="process.php" method="POST">
<input type="submit" id="subm" class=" submit_button" value="Submit" >
</form>
$("#subm").click(function() {
alert('Doesnt work');
}
$("#my_form").submit(function() {
alert('Doesnt work');
}
Upvotes: 1
Views: 864
Reputation: 72299
First you need to add Jquery library to work and then you have to use preventDefault()
like below:-
<form id="my_form" action="process.php" method="POST">
<input type="submit" id="subm" class=" submit_button" value="Submit" >
</form>
<script src = "https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type = "text/javascript">
$(document).ready(function(){ // After document is completly loaded
$("#subm").click(function(e) {
e.preventDefault(); // prevent the form submit on click
alert('Does work'); //alert the message
});// you missed ); here
});
</script>
Note:- check your browser development console and you will exactly get what errors are there.
$(document).ready(function(){ // After document is completly loaded
$("#subm").click(function(e) {
e.preventDefault(); // prevent the form submit on click
alert('Does work'); //alert the message
});// you missed ); here
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="my_form" action="process.php" method="POST">
<input type="submit" id="subm" class=" submit_button" value="Submit" >
</form>
Upvotes: 1