Tried this :
<a href="#" id="showAlert">Click me</a>
<div id="pasteContent"></div>
var elemFunc = '<script>alert("myString");</script>';
$("#showAlert").click(function () {
$("#pasteContent").html(elemFunc);
});
What I’d like to do is to append the string alert(myString); (which is a script) that must be executed… how can I do? Is it not correct?
Add a backslash before
/in</script>: http://jsfiddle.net/kxALH/3/Your code failed, because
</script>is considered as a close tag for your fiddle script. As a result, your first fiddle looked somewhat weird.Note: If you want to execute an arbitrary string of code,
$.globalEval('alert("myString")');may suit better.