The System Restart
The System Restart

Reputation: 2881

Wrapping some part of html using jQuery

I've 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>

In 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 using jQuery? Please help me on that.

Upvotes: 4

Views: 73

Answers (4)

Shikiryu
Shikiryu

Reputation: 10219

With one line (or 3 for clarity):

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

JSFIDDLE

Upvotes: 0

abu taha9
abu taha9

Reputation: 55

read this link it may help you :What is the Most effcient way to create html elements using jquery

My Answer is the answer well be like this :

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);

Upvotes: 0

epascarello
epascarello

Reputation: 207501

Well if the contents will always be the same, you can do something like

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

http://jsfiddle.net/Ysq48/

Upvotes: 4

Adil Shaikh
Adil Shaikh

Reputation: 44740

Demo

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

Upvotes: 0

Related Questions