blue-sky
blue-sky

Reputation: 53876

Error : "Uncaught Typeerror: cannot read property 'innerHTML' of null" when appending a new <script> to div

In below fiddle I am receiving this error : Uncaught TypeError: Cannot set property 'innerHTML' of null

Looking at the code it the error seems to be thrown here :

_$("pds-answer6343621").innerHTML=y6343621

I think this is occuring because the script is executing before the document is parsed but since the function is being called within $(document).ready it should work ?

How can fiddle be modified so that script is loaded when function update() is invoked http://jsfiddle.net/m7q3H/121/

Fiddle code :

   <body>
    <div id="toupdate">
     <div id="toremove">
<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/5968383.js"></script>
<noscript>
    </div>
    </div>
    </body>​

$(document).ready(function() {
update();
                     });

    function update(){
            $('#toremove').remove();
        alert('removed');
$('#toupdate').append('<div id="toupdate"><script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/6343621.js"></scr'+'ipt></div>');

    }

​

Upvotes: 1

Views: 3778

Answers (4)

Dr.Molle
Dr.Molle

Reputation: 117354

The external script makes use of document.write()

Therefore you cannot inject the script after the document has been loaded, e.g. on domReady

You may try to use an iframe for the poll to be able to update it.

When you only need to update the poll-results, use $.getScript (without removing #toremove)

Upvotes: 1

Dariush Jafari
Dariush Jafari

Reputation: 5443

because the y6343621 is null.

if ( y6343621 ==null)
  y6343621="";

Upvotes: 0

sakabako
sakabako

Reputation: 1150

Your problem is in the HTML head. You are including the script there, then adding it again with JavaScript. Try taking it out of the head.

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68440

The problem is that you're adding http://static.polldaddy.com/p/6343621.js twice. Because of this, there are 2 elements with id = pds-answer6343621, so _$("pds-answer6343621") returns an array of 2 elements instead of a single object. That's why it it fails,

Why are you adding this file twice?

Upvotes: 2

Related Questions