views:

77

answers:

2

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>
+2  A: 

The document has already been loaded (the snippet you insert is not a document), so your callback fires immediately. Try this:

<script type="text/javascript">
    alert($("#navnet").val());
</script>

Alternatively define a function and call it explicitly after the html has been inserted:

<script type="text/javascript">
    var mycallback = function() { alert($("#navnet").val()); }
</script>

...    

.html(result);
if (typeof window.mycallback == 'function') mycallback();

If possible, better approach would be getting rid of the <script> element and putting all your code in success callback.

jholster
I had to reset the callback before each ajax call, as this function is used a lot of places where there is no JS after show.
devzero
Added a typeof check in the example, will that help?
jholster
Actually no, what happens is that when I reload the dialog from a different place, the html returned does not overwrite the mycallback. Therefore the second time I use the menuShowModal() the mycallback gives an alert "undefined". If I instead set it to null before the ajax call it works as expected. Se the edit I made to the question for the complete solution.
devzero
A: 

I think the problem is that until you call dialog() the html just floats around in memory and is not appended to the DOM so you can't find elements in it by ID.

This should work:

 $('#modalDialog').dialog('close');
 var $dialog = $('<div id=\'modalDialog\'></div>').css('display','none');
 $(document.body).append($dialog);
 $dialog.html(result)
 .dialog({
    autoOpen: true,
    title: 'Basic Dialog',
    modal: true
 });
Wikeno
You should also remove the appended $dialog from the DOM after the dialog was closed
Wikeno