Balvinder Rayat Balvinder Rayat - 3 months ago 21
jQuery Question

each loop for insertafter failing

I am trying to add

<span class='validation'></span>
after each
<label class="control-label">
. I have tried following but it's adding span class multiple times after each label.

I have also tried using
.one()
after control label class but didn't worked.

I want to add same class for each select as well. will adding select inside
$('#myform :input')
work?

what is wrong in this code.

$('#myform :input').each(function(){
$( "<span class='validation'></span>").insertAfter(".control-label");
});


HTML

<div class="col-sm-12">
<label for="Rec_Name" class="control-label">Your Name :</label>
<input class="form-control" type="text" id="Rec_Name" name="Rec_Name" placeholder="Enter Your Name">
</div>

Answer

Because you add span to after each control-label class.

Try like this:

$('<span>Validation</span>').insertAfter($(this).parent().find('.control-label'));

Maybe a little crazy but should work. Hope this helps.