views:

7284

answers:

4

hi, i have div:

<div id="example">
  ...some text...

  <script type="text/javascript">

    ... some javascript...
  </script>
</div>

how to get content of div "example" but also with that javascript ? $("#example").html(), $("#example").text(), $("#example").val() doesnt work.

so i want to get this:

 ...some text...

  <script type="text/javascript">

    ... some javascript...
  </script>

how to do that ? thanks

+1  A: 

Working Demo

You can use

html(): Get the html contents (innerHTML) of the first matched element.

var contents = $("#example").html();
rahul
when i use $("#example").html(); i get only content of div, but without that javascript, it ignores all javascript
mm
+4  A: 

The html() method should work for you. Are you sure you are running the code after the DOM is completed?

$(document).ready(function(){
   alert($("#example").html());
});
Manticore
A: 

Just use:

$("#example").get().innerHTML;

That gets the DOM object from the jQuery object and spits out the raw content.

aditya
A: 

Just tried it and $('#example').html() does work in isolation:

<html>
<head>    
        <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js' lang='text/javascript' />    
</head>
<body>
<div id="example">
  ...some text...
  <script type="text/javascript">
    ... some javascript...
  </script>
</div>

<script>
alert($('#example').html());
</script>

</body>
</html>
ndp