Reputation: 860
I'd like to be able to detect which form on a page was submitted, grab its ID and assign it to a variable. How can I do this?
Thank you!
Upvotes: 0
Views: 132
Reputation: 15220
I assume you're working with jQuery. You can easily do this:
<form id="form1" ...>
...
<input type="submit" />
</form>
<form id="form2" ...>
...
<input type="submit" />
</form>
<script>
$('form').submit(function(){
var form_id = $(this).attr('id'); //here you grab the id
alert('form '+form_id+' was submitted!');
return false; //prevent from 'really' submitting
});
</script>
Note that this variable will only be available within that JavaScript-code and only until the page is left/reloaded. If you want to save it permanently, you could e.g. post it to the server and then save it to the session / a database / a cookie / whatever.
Upvotes: 1