Reputation: 1
i want to execute jquery scripts in jquery html method. Like
$(body).html("<div id='mydiv'></div><script>$('#mydiv').fadeIn();</script>");
or
$(body).append("<div id='mydiv'></div><script>$('#mydiv').fadeIn();</script>");
div is inserted but javascript code not run.
How code can be runned?
Upvotes: 0
Views: 92
Reputation: 5135
You should try something like this:
$(body).append("<div id='mydiv'></div>");
$('#mydiv').fadeIn();
After first line executing there are mydiv
appears in DOM, so you can access it directly from your current script.
If you want add script to you DOM using jQuery you should do it in some tricky way:
$("<script>$('#mydiv').fadeIn();</" + "script>").appendTo(document.body);
or
$("\x3Cscript>$('#mydiv').fadeIn();\x3C/script>").appendTo(document.body);
Without escaping or breaking </script>
tag it terminates the entire script.
If you have more issues with your code please refer to next answer https://stackoverflow.com/a/3603496/1430055 where you can find explanations about debugging such a dynamic script, etc.
Upvotes: 2