0

Possible Duplicate:
Get selected element’s outer HTML
show a string of my div and append

With the code below I need to alert this <div id="myDiv"><h1>test</h1></div>

but my code alerts this <h1>test</h1>

How do I do this? thanks

Here is my fiddle http://jsfiddle.net/5GCEQ/

        <div id="myDiv">

            <div>test</div>
            <div>test</div>
            <div>test</div>
            <div>test</div>
            <div>test</div>
            <div>test</div>
            <div>test</div>

        </div>​

    var s = $('#myDiv').html('<h1>test</h1>');

    alert(s.html()); //alerts <h1>test</h1>

//
//but I want it to alert    <div id="myDiv"><h1>test</h1></div>
//​​​
Community
  • 1
  • 1
Hello-World
  • 9,277
  • 23
  • 88
  • 154

2 Answers2

2

Use this:

alert(s[0].outerHTML);
keaukraine
  • 5,315
  • 29
  • 54
1

You can use parent() in JQuery:

var s = $('#myDiv').html('<h1>test</h1>');
alert(s.parent().html());

Here is the jsfiddle code.

AlexStack
  • 16,766
  • 21
  • 72
  • 104