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?
-- solution from below post is a callback function, here is the working code --
function menuShowModal(a) {
$.ajax(
{
url: a.href,
success: function (result) {
onDialogBoxShown = null;
$('#modalDialog').dialog('close').remove();
var $dialog = $('<div id=\'modalDialog\'></div>')
.html(result)
.dialog({
autoOpen: true,
title: 'Basic Dialog',
modal: true
});
if (onDialogBoxShown != null) onDialogBoxShown();
},
cache: false,
type: 'get'
});
return false;
}
<input type="text" id="navnet" value="test" />
<script type="text/javascript">
var onDialogBoxShown = function () { alert($("#navnet").val()); }
</script>