Alex Rivera | Logout

Difference between innerHTML and .html() from jQuery

Asked 2011-03-22T11:35:40.863
14

Can somebody tell what is the difference between jquery .html() function and innerHTML?

<script type="text/javascript">
$(document).ready(function(){
            $('#test_link').click(function(){

                //$('#div_test_out').html("<div width='250px' height='100px' id='div_test'><script language='javascript'>alert('insider');<\/script>asddsa</div>");
                document.getElementById('div_test_out').innerHTML="<div width='250px' height='100px' id='div_test'><script language='javascript'>alert('insider');<\/script>asddsa</div>";      
            });
});
</script>
<a href="#" id="test_link" >TEST LINK :-)</a><br/><br/>
<div width="100px" height="100px" id="div_test_out"></div>

When I use first option, that is jQuery, script inside runs, and alert shows up, but if I use second option that with the innerHTML (which I though is the same and there is no difference between them), script is not working ;-(

What could be the cause?

Edit
Report

1 Answer

7

Setting the innerHTML property does not execute scripts.

jQuery contains special code to execute scripts for you.

answered 2011-03-22T11:39:24.003

Your Answer