How to use jquery ajax to set the content of a div, then call JS from that div
- by devzero
I'm using JQuery to load the content of an mvc usercontroll:
function menuShowModal(a) {
$.ajax(
{
url: a.href,
success: function (result) {
$('#modalDialog').dialog('close');
var $dialog = $('<div id=\'modalDialog\'></div>')
.html(result)
.dialog({
autoOpen: true,
title: 'Basic Dialog',
modal: true
});
},
cache: false,
type: 'get'
});
return false;
}
The returned HTML looks like this:
<input type="text" id="navnet" value="test" />
<script type="text/javascript">
$(document).ready(
function () {
alert($("#navnet").val());
}
)
</script>
The problem is that the alert returns "undefined" and not "test" as it should, in other words the JS is executed before the html is inserted, how do I work around this?