See the HTML code below.
$(function () {
$('input[type=button]').on('click', function() {
formId = $(this).parent("form").attr("id");
$('#flash').html('Botão do ' + formId + ' pressionado.');
});
});
input[type='button'] {
margin: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="form1">
<input type="button" value="Click" />
</form>
<form id="form2">
<input type="button" value="Click" />
</form>
<div id="flash"></div>
Press "Run" to see this code snippet working.
There are two equal forms, each with its id . In JavaScript, with jQuery, assigns a click event on the buttons, thus reaching both forms. That is, the behavior of the two will be exactly the same. With the parent
function, I look for the form
element that the button is pressed on and, thus, identify which form was used. To check, just see the message that appears in div #flash
.
The CSS snippet was just to make the look a bit cleaner. It is not fundamental to the workings of logic.