How can an external javascript file be injected into a div. When I try to set it using below code, the div ‘toupdate’ is not updated with new javascript file.
<script type="text/javascript">
$(document).ready(function () {
$('#toupdate').html('<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/5968383.js"></script>');
});
</script>
<body>
<div id="toupdate">
<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/6343621.js"></script>
</div>
</body>
Your replacement code is being executed before the div ‘toupdate’ has loaded, you either need to move the
<script>tag to after the div or put you replacement code within a$(document).ready()function like so:EDIT:
tested and working example:
JSfiddle: http://jsfiddle.net/m7q3H/45/