0

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

3
  • I don't understand the question. Commented May 13, 2010 at 10:50
  • @cletus - i added in what i am trying to hopefully explain the issue better. let me know if this helps Commented May 13, 2010 at 10:53
  • Perhaps it would be better if you explained what you were trying to do and why. Commented May 13, 2010 at 10:55

2 Answers 2

6

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')
Sign up to request clarification or add additional context in comments.

2 Comments

parentDiv is already a jQuery object, no need to clone it :)
I stand corrected, I was thinking about the "this" which should be encapsulate in $() since it otherwise does not work in some IE versions...
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.