mdesantis
mdesantis

Reputation: 8517

Get form from the associated input element

Saying that I have this HTML form:

<form id="form">
  <input id="input_el" type="text">
  <input id="submit_el" type="submit">
</form>

I want to get the form element starting from one of its input elements, so I could write:

<script type="text/javascript">
$('#input_el').form().validate();
$('#submit_el').form().submit();
</script>

That is, $('#input_el').form() should return $('#form') . Is that possible?

Thanks!

Upvotes: 0

Views: 383

Answers (3)

Tom Maeckelberghe
Tom Maeckelberghe

Reputation: 1999

$('#input_el').parent('form')

or

$('#input_el').parent()

Upvotes: 0

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17762

Try:

$("input").closest("form").validate().submit();

Upvotes: 1

Nikhil
Nikhil

Reputation: 3384

You can easily do

$('#input_el').closest('form')

Upvotes: 2

Related Questions