I have the following
<div><span class="myspan">hello</span> this text has no span </div>
In jQuery, how can I wrap a div
or span around the "this text has no span" text?
jQuery wrap() method is used to wrap specified HTML elements around each selected element. The wrap () function can accept any string or object that could be passed through the $() factory function.
The wrap() method wraps specified HTML element(s) around each selected element.
The wrapped set is simply a list of DOM elements(with their children) in the order in which they are defined in the current document that matches a selector or in the order in which they have been created on the fly with the $(html) function.
For your current example here is my one line solution:
$("div span").detach().prependTo($("div").contents().wrap("<span />").end());
DEMO: http://jsfiddle.net/awwTA/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With