dexter
dexter

Reputation: 1207

copy html div in javascript variable using jquery

in my aspx page i have a div which contains simple html ,has some id(say 'datadiv')

using jquery i want to get the html of that div (entire div) into a JavaScript variable

how can it be done?

thank you.

Upvotes: 2

Views: 15345

Answers (4)

kjagiello
kjagiello

Reputation: 8410

var someVar = $("#somediv").html();

Upvotes: 9

Abhishek Goel
Abhishek Goel

Reputation: 19751

Finally got it working..!! the simplest way !!
HTML Code

<div class="extra-div">
    <div class="div-to-pull-in-variable">
     // Your Content Here
    </div>
</div>

JQuery Code

    $(function() {
        var completeDiv = $('.extra-div').html();
        alert(completeDiv); 
       })
    });

Upvotes: 0

Dinesh Rabara
Dinesh Rabara

Reputation: 1127

            <!DOCTYPE html>
            <html>
            <head>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
            </head>
            <body>

            <b>Hello</b><p>, how are you?</p>

            <script>
            $("b").clone().prependTo("p");
            </script>

            </body>
            </html>
            **Out put:**
            Hello
            Hello, how are you?

Upvotes: 0

Karl B
Karl B

Reputation: 1597

If you want the equivalent of Internet Explorer's outerHTML to retrieve both the div and it's contents then the function from this page that extends JQuery should help:#

jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
};

Then call:

var html = $('#datadiv').outerHTML();

Upvotes: 2

Related Questions