| 3:48pm To 2013-03-16...">

Wrapping some html part with jQuery

I have something like:

<div class="the_notice">
    <span class="time"> | 3:48pm</span>
    To <strong>2013-03-16_10-56-33</strong> By <strong>XYX</strong>
</div>

As a result, I want something like:

<div class="the_notice">
    <span class="time"> | 3:48pm</span>
    <div class="wrap">
      To <strong>2013-03-16_10-56-33</strong> By <strong>XYX</strong>
    </div>
</div>

How can I achieve this with jQuery? Please help me with this.

+5
source share
4 answers

Well, if the content is always the same, you can do something like

$('.the_notice').contents().slice(2).wrapAll('<div class="wrap"/>');

http://jsfiddle.net/Ysq48/

+5
source

Demo

   var temp = $('.time');
   $('.time').remove();
   $(".the_notice").contents().wrapAll('<div class="wrap" />');
   $(".the_notice").prepend(temp);
0
source

read this link, this may help you: What is the most efficient way to create html elements with jquery

My answer is the answer, which will be as follows:

var myTimeIsHere = //calculate your time here;
var x = //calculate x here.;
var y = //calculate y here.;

var newHTML = "<span class='time'>" |+ myTimeIsHere+"</span>"+
              "<div class='wrap'>To <strong> "+x+" </strong>"+
              "by "+y+"</div>";
$(".the_notice").html(newHTML);
0
source

With one line (or 3 for clarity):

$('.the_notice')
     .wrapInner($('<div class="wrap">'))
     .prepend($('.the_notice .time').detach());

JSFIDDLE

0
source

All Articles