Ad

Uncaught TypeError: $(...)[0].firstChild.wrap Is Not A Function

- 1 answer

I need quick help with this. I am trying to wrap first node of label with <span></span> but it gives error Uncaught TypeError: $(...)[0].firstChild.wrap is not a function

$('.lbl')[0].firstChild.wrap('<span></span>');

However If I try just following its selects but its has issue with .wrap

$('.lbl')[0].firstChild

Following is the HTML

<label class="lbl">
some text
<input type="text">
</label>
<label class="lbl">
some text
<input type="text">
</label>
<label class="lbl">
some text
<input type="text">
</label>
<label class="lbl">
some text
<input type="text">
</label>

Thanks

Ad

Answer

Assuming you are wanting to wrap the text node, you need to wrap the whole node in $() in order to use jQuery methods

$($('.lbl')[0].firstChild).wrap('<span></span>');
span{color:red}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="lbl">
some text
<input type="text">
</label>
<label class="lbl">
some text
<input type="text">
</label>
<label class="lbl">
some text
<input type="text">
</label>
<label class="lbl">
some text
<input type="text">
</label>

Ad
source: stackoverflow.com
Ad