user2095956
user2095956

Reputation: 433

How To merge two divs to one div

I have two divs

<div id = "first">some details111</div>

and

<div id = "second">some details222</div>

I want to create:

<div id ="New">some details111 some details222</div>

What is the best and the fast way to do it?

Upvotes: 3

Views: 17815

Answers (5)

youssDev
youssDev

Reputation: 90

$("&lt;div&gt;&lt;/div&gt;").attr("id", "New").html($("#first").html() + $("#second").html()).appendTo($("body"));

Upvotes: 0

Nick Tomlin
Nick Tomlin

Reputation: 29271

Some vanilla JS for kicks and giggles:

// grab the content from our two divs
var content1 = document.getElementById('one').innerHTML;
var content2 = document.getElementById('two').innerHTML;

// create our new div, pop the content in it, and give it an id
var combined = document.createElement('div');
combined.innerHTML = content1 + " " + content2; // a little spacing
combined.id = 'new';

// 'container' can be whatever your containing element is
document.getElementById('container').appendChild(combined);

Upvotes: 3

Pandian
Pandian

Reputation: 9136

Try the below :

Fiddle Example : http://jsfiddle.net/RYh7U/99/

If you already have a DIV with ID "NEW" then try like below:

$('#New').html($('#first').html() + " " + $('#second').html())

If you want to Create a div and then add the Content then try like below.

$("body").append("<div id ='New'></div>")
$('#New').html($('#first').html() + " " + $('#second').html())

Upvotes: 1

Well, using jQuery you can do by this way:

$("body").append(
    $('<div/>')
        .attr("id","New")
        .html(
            $("#first).html() + $("#second").html()
        )
);

Upvotes: 0

red_alert
red_alert

Reputation: 1728

Using jQuery you could do that:

$(document).ready(function(){
   $("body").append("<div id='New'></div>");
   $("#New").text($("#first").text() + " " +$("#second").text());
});

Upvotes: 5

Related Questions