Search code examples
jqueryinsertafter

How to add label for input field using before and after jquery?


I want to add a label before and after the input field. I need this result.

`<label class="containertitle">
<input id="button" type="checkbox" name="AllSalesArea"  >
<span class="checkmarktitle"></span>
</label>`

but I received an output like this.

<label class="containertitle"></label>
<input id="button" type="checkbox" name="AllSalesArea">
<span class="checkmarktitle"></span>

This is my code. I need the label close tag at the end of the span.

<input id="button" type="checkbox" name="AllSalesArea"  >


$('input[name="AllSalesArea"]').before('<label class="containertitle'>)
$('input[name="AllSalesArea"]').after('<span class="checkmarktitle"></span></label>')

Solution

  • Thanks to @freedomn-m.

    $('input[name="AllSalesArea"]' ).wrap( '<label class="containertitle"></label>' );
    $('input[name="AllSalesArea"]').after('<span class="checkmarktitle"></span>');