1

Why is below function update() not updating ? Is the structure of this fiddle correct ?

fiddle code :

HTML:

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

JavaScript:

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

function update(){
    $('#toupdate').remove();
    alert('removed');
    $('body').append('<div id="toupdate">
        <script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/6343621.js"></script>
    </div>');
}
kojiro
  • 74,557
  • 19
  • 143
  • 201
blue-sky
  • 51,962
  • 152
  • 427
  • 752

1 Answers1

7

You can't have new lines in a JavaScript string like that. Also the </script> maybe causing the JS to stop.

$('body').append('<div id="toupdate"><script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/6343621.js"></scr'+'ipt></div>');
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 4
    some additional resources: [script tag as string](http://stackoverflow.com/questions/1659749/script-tag-in-javascript-string) and [whitespace and semicolon insertion](http://en.wikipedia.org/wiki/JavaScript_syntax#Whitespace_and_semicolons) – jbabey Aug 16 '12 at 19:51