I have a webpage with multiple forms, each with it’s own controls.
I want to use the “onclick” event on the controls to run a JavaScript function.
Is there some way to tell which form contained the control that the “onclick” event came from?
I would like to do it without using the id of the form, but it’s OK if I have to.
(I want to do a submit, on which ever the form it came from.)
Thanks!
You can get the form attributed to any input element by simply checking for it’s
.formlike so:For the element just set
onclick="getForm(this);"jsFiddle Demo – open console to see the “el” being passed in, as well as the parent form
On a side note: I’d recommend not using onclick / onmouseover / any “JS” events within your HTML elements. Not only should these different things be separated in general, but in IE8 and lower, every single one of these events turns into a small script block element, which slows down performance.