15

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?

kuba
  • 1,019
  • 1
  • 18
  • 39

3 Answers3

11

jQuery's .html() method is a multipurpose function for accessing and manipulating innerHTML. When used as a setter, it returns the jQuery collection for chaining. When used as a getter, it returns the markup representation of the collection elements' innards.

When you use it as a setter--to write markup into an element--jQuery reads the markup and extracts scripts from within. It then adds them to the DOM separately in a manner that causes their execution. .html() implicitly causes a few operations (script handling being one) whereas writing to innerHTML simply causes the innerHTML to change, but very little is done with that HTML.

JAAulde
  • 19,250
  • 5
  • 52
  • 63
  • OK, but where this extraction is in jQuery library code? I didn't found it in .html() (maybe this is because I'm not really Javascript kungfu master:)) I'm just trying to understand this case as much I can :) – kuba Mar 22 '11 at 12:45
  • .html() uses .append() which uses domManip() which does the script sniffing and execution. – JAAulde Mar 22 '11 at 12:45
  • 1
    Ok, It seems that this domManip() is the key function :-) Thank you :-) – kuba Mar 22 '11 at 13:05
8

Setting the innerHTML property does not execute scripts.

jQuery contains special code to execute scripts for you.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I have looked into the source, but I couldn't find anything special in there that would execute scripts. As a matter of fact, it tries to used innerHTML itself. – Ikke Mar 22 '11 at 11:44
  • Yes, exactly, there is also comment inside .html() that says that they first check if they can use just simple innerHTML – kuba Mar 22 '11 at 12:41
  • @Ikke .html() uses .append() which uses domManip() which does the script sniffing and execution. – JAAulde Mar 22 '11 at 12:44
  • @JAAulde Ah, right. It checks with the rnocache pattern if there is a scripttag inside, and skips the innerHTML part. – Ikke Mar 22 '11 at 14:09
-1

I think this is the correct way:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" ></script>

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

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

Why you insert java script alert in to div?

Faraona
  • 1,685
  • 2
  • 22
  • 33
  • alert is for testing purpose only, there is other JS code instead alert. Whole idea is not mine, I'm just refactoring the code:) – kuba Mar 22 '11 at 11:50