Search code examples
jqueryinputappendprependinsertafter

Problem with jQuery


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


Solution

  • 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 */