I have created a page with two forms. Each form has a text box and a submit button. When either form is fired, I want the (same) submit function to be fired. The function must then be able to tell which form was fired, getting the entered text as well as an ID.
It works for the first form, but not for the second form. When I click the second Submit button, it correctly gets the id="1", but it reads the input from the first text box. In general I'm having a hard time navigating targets: sometimes it's only the first of the elements that causes the firing and sometimes it's all.
<div>
<p>First form</p>
</div>
<div class = 'add_video_form' id = "3">
<form method="post" action="dummy/">
<input type="text" id="url">
<input type="submit" value = "Add video">
</form>
</div>
<div>
<p>Second form</p>
</div>
<div class = 'add_video_form' id = "1">
<form method="post" action="dummy/">
<input type="text" id="url">
<input type="submit" value = "Add video">
</form>
</div>
The script is:
$(document).ready(function(){
$("form").submit(function() {
var v_new_url = $("input#url").val();
var v_cat_id = $(event.target).parents(".add_video_form").attr("id");
alert(v_new_url);
alert(v_cat_id);
return false;
});
}); //end of $(document).ready