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
  • 8,457
  • 22
  • 78
  • 151

2 Answers2

2

Use this:

alert(s[0].outerHTML);
keaukraine
  • 5,287
  • 26
  • 53
1

You can use parent() in JQuery:

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

Here is the jsfiddle code.

AlexStack
  • 15,377
  • 18
  • 69
  • 102