'Stop two or more click events from the same click event
I have created a button in jsp page and i have written two click functions for the same button, one with the tag and the other with the class/id of the button. Now, when i click on the button, both the click functions are getting called one by one. My requirement is that inside the first click function, i want to stop the second click function. I do not want to remove the remove the second click function as in some cases i need it to execute.
Below is the code i'm trying.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("input[type=button]").click(function(e){
if(true) {
alert("The paragraph was clicked.");
}
else {
// I want to remove the second click function here
}
});
$("#test").click(function(){
alert("Test paragraph was clicked.");
});
});
</script>
</head>
<body>
<input type = 'button' value='test' id='test'/>
</body>
</html>
Solution 1:[1]
You need to use e.stopImmediatePropagation()
:
$("input[type=button]").click(function(evt){
evt.stopImmediatePropagation();
....
});
This prevents "sideways bubbling", for want of a better phrase, i.e. further event callbacks bound to the same element from firing.
Solution 2:[2]
You should try to put
return false;
in the else statement. Since it stops the propagation of the event it should do what you want.
Solution 3:[3]
Description: Attach a handler to an event for the elements. The handler is executed at most once per element.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | 317 |
Solution 3 | Mannepalli Baburao |