leora
leora

Reputation: 196499

reference a form inside a selector in jquery

if i have this code in jquery:

var parentDiv = $(this).parent('.copyInstance');

inside this div there is a form.

how do i reference a form element inside this selector? i want something like this

  $.post($(parentDiv + " Form").attr('action'), $(parentDiv + " Form").serialize(),  function(data) {
    });

but this above doesn't seem to work

Upvotes: 0

Views: 14479

Answers (2)

alexteg
alexteg

Reputation: 2857

To reference a children element you can use .find() for example:

$.post(parentDiv.find('form').attr('action'), 
       parentDiv.find('form').serialize(),
    function(data) {
    });

Depending on how it is structured you could also use .children() like:

parentDiv.children('form')

Upvotes: 6

Nick Craver
Nick Craver

Reputation: 630409

You have 3 options for this selector:

parentDiv.find('form') //finds any form at any level deep inside
parentDiv.children('form') //finds forms that are immediate children
$("form", parentDiv) //really calls .find() but easier on the eyes maybe?

Here's more info on .find(), .children() and jQuery(selector, context).

Upvotes: 2

Related Questions