views:

55

answers:

3
if ( !this.labelContainer.append(label).length )
 this.settings.errorPlacement
  ? this.settings.errorPlacement(label, $(element) )
  : label.insertAfter(element);

(this code is from validate plugin, it repeats for each input)

label is <label>text</label>

element is <input />

This works good, it adds label directly after the element.

Looks like this:

<dd>
    <div>
        <input />
        <label>text</label>
    </div>
</dd>

How to place label after the <div> (element's parent), not after the element?

Full code of the script:

http://www.jsfiddle.net/YSSCE/1/ (search for label.insertAfter(element))

A: 

element.parent gets the parent to the element.

label.insertAfter(element.parent);

or possibly

label.insertAfter($(element).parent);
David Mårtensson
A: 

Change label.insertAfter(element) to label.insertAfter(element.parent())

But this could cause problems if your HTML markup changes.

laurencek
Doesn't work. Uploaded full code http://www.jsfiddle.net/YSSCE/1/
Happy
It was assuming the element is a jQuery object. This should have been assumed as the parent() function is a jQuery function.
laurencek
+2  A: 

Use insertAfter. Example on jsFiddle.

$("<label />").text("Label").insertAfter(
    $("input").parent()
);

Updating your code (v2):

this.settings.errorPlacement
      /* not sure what this method do, maybe you need to add .parent() as well*/
    ? this.settings.errorPlacement(label, $(element) )
    : label.insertAfter($(element).parent()); /* insert after the parent */
BrunoLM
Can you update your solution to my code? Here is a full file http://www.jsfiddle.net/YSSCE/1/
Happy