Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery wrap span around text

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?

like image 695
LeBlaireau Avatar asked Nov 23 '12 11:11

LeBlaireau


People also ask

What does the jQuery wrap () function do?

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.

What is wrap in javascript?

The wrap() method wraps specified HTML element(s) around each selected element.

What is wrapped set in jQuery?

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.


1 Answers

For your current example here is my one line solution:

$("div span").detach().prependTo($("div").contents().wrap("<span />").end());

DEMO: http://jsfiddle.net/awwTA/

like image 82
VisioN Avatar answered Sep 19 '22 01:09

VisioN