Ciro Vargas
Ciro Vargas

Reputation: 422

Append an element after other

I want to select an element and append this after other:

Using this dont work:

$('.sub'+$(this).val()).appendTo($(this).parent().parent().parent().parent());

the dom is:

<div class="sub1"></div>


<label></label>

Expect this:

<label></label>
<div class="sub1"></div>

the really dom:

http://pastebin.com/6Vd6USEp

the task is append the div after the label, i search this label per a input.subN and pick de parentparentparentparentparent

Upvotes: 0

Views: 84

Answers (3)

Luke Jakimowicz
Luke Jakimowicz

Reputation: 380

Not sure about what you're trying to do, but here goes: If you need to simply move the label to be before the div, do this:

$("label").remove().insertBefore(".sub1");​

If you need to move the div to be after label, do this:

$(".sub1").remove().insertAfter("label");​

See here: http://jsfiddle.net/QpP9J/

Upvotes: 2

Adil
Adil

Reputation: 148150

You need before().

Live Demo

$('.sub1').before('<label></label>');

Upvotes: 1

Ravi Y
Ravi Y

Reputation: 4376

if you just want to add your label before the other element, use prepend. http://api.jquery.com/prepend/

Upvotes: 1

Related Questions