Possible Duplicate:
Calling a JavaScript function returned from a Ajax response
var url = "showpdf.php";
$.ajax({
type: "post",
url: url,
data: {academic:academic,uni:uni,course:course,lan:lan,sem:sem,subject:subject,type:type,clz:clz},
success: function(response)
{
alert(response);
document.getElementById("alldata").innerHTML = response;
}
});
inside response i have bellow html code with simple JavaScript function
<label onclick="popup()"> clickme </label>
<script>
function popup()
{
alert("hello");
}
</script>
here, this popup()function is not working please help me.
The problem is you expect that Javascript contained within an ajax response is executed. This isn’t the case, the browser doesn’t execute any Javascript contained within ajax responses. It might be possible to try to parse out the Javascript and execute it in some way, such as
eval()but that would be nasty and not a good idea. Usingeval()you also have to consider that it will only accept valid Javascript, so you couldn’t just pass your response to it because that includes some HTML.A possible solution could be to have the
popup()function already defined in the page or an external Javascript file, and to assign the click handler after you add the HTML to the DOM.For example: