Wrap Content in a DIV

simple question i think but can't figure it out

How can I wrap specific text in a DIV?

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

And you need to have this:

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

thanks for the help

+3
source share
3 answers

You can use the 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 above code:

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");
+2
source
var str = 'Price:<strong>£12.30 (Ex VAT)</strong>';
var div = $('<div />').html(str);
0
source

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");
0
source

Source: https://habr.com/ru/post/1719225/


All Articles