The submit event is triggered when a submit button is clicked, and there could be more than one, a button element without type="button|reset" is clicked. It can also be triggered by the Enter key.
You can use this to determine if the logout button was clicked. However, for form submission purposes, the submit event is by far the most reliable.:
$("#logout-btn").on('click', function(e) {
e.preventDefault(); //prevents default action.
alert("The logout button was clicked.");
});
$(function() {
$("#logout-btn").on('click', function(e) {
e.preventDefault(); //prevents default action.
alert("The logout button was clicked.");
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="login-form">
<input placeholder="Username" id="formUsr" required/>
<input type="password" placeholder="Password" id="formPwd" required/>
<input id="login-btn" type="submit" value="Login"/>
<input id="logout-btn" type="submit" value="Logout"/>
</form><!-- login form -->
Another approach:
$(function() {
$(':submit').on('click', function(e) {
e.preventDefault();
if( $(this).is('#login-btn') ) {
alert('login-btn clicked');
} else if( $(this).is('#logout-btn') ) {
alert('logout-btn clicked');
} else {
alert('some other submit button clicked');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="login-form">
<input placeholder="Username" id="formUsr" required/>
<input type="password" placeholder="Password" id="formPwd" required/>
<input id="login-btn" type="submit" value="Login"/>
<input id="logout-btn" type="submit" value="Logout"/>
</form><!-- login form -->