views:

45

answers:

2

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

+1  A: 

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')
alexteg
`parentDiv` is already a jQuery object, no need to clone it :)
Nick Craver
I stand corrected, I was thinking about the "this" which should be encapsulate in $() since it otherwise does not work in some IE versions...
alexteg
+1  A: 

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).

Nick Craver