Max Frazier
Max Frazier

Reputation: 37

Select parent through child element via ID in jquery?

I have a button in a form with a unique ID. I'm trying to select the form that button is in using the ID of the button. Something like:

    $('#submitButton').parent;

But that doesn't work.

Upvotes: 0

Views: 45

Answers (1)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93611

jQuery's parent is a function:

var $parent = $('#submitButton').parent();

You will find most of the jQuery API is made of functions and not properties.

You can also get at the underlying DOM object (and therefore its properties) using $('#submitButton')[0].somepropertyname but not recommended if you are already using jQuery.

based on the wording of your question you probably should use closest instead:

e.g.

var $form = $('#submitButton').closest('form');

jQuery's closest() returns the nearest matching object from the list of ancestors and the object itself.

Upvotes: 1

Related Questions