tags:

views:

160

answers:

3

Hi, simple question i think, but cant figure this out

How can I wrap particular text into DIV?

Have this Price:<strong>£12.30 (Ex VAT)</strong>

And need to have this:

<div>Price:<strong>£12.30 (Ex VAT)</strong></div>

Thanks for your help

+1  A: 

You can use appendChild method :

var newDiv = document.createElement('div');

newDiv.innerHTML = "Price:<strong>£12.30 (Ex VAT)</strong>";

document.getElementById('parentDiv').appendChild(newDiv);

Here is the JQuery equvalent of the code above :

var newDiv = $("<div>Price:<strong>£12.30 (Ex VAT)</strong></div>");
$('#parentElement').append(newDiv);

Or :

$("<div>Price:<strong>£12.30 (Ex VAT)</strong></div>").appendTo("#parentElement");
Canavar
Looking for jQuery method please?
Dom
A: 
var str = 'Price:<strong>£12.30 (Ex VAT)</strong>';
var div = $('<div />').html(str);
eyelidlessness
A: 

jQuery has a wrap function that does exactly what you are describing...

 var str = "Price: <strong>£12.30 (Ex VAT)</strong>";

 $(str).wrap("<div></div>").appendTo("body");
Josh Stodola