How to use jquery ajax to set the content of a div, then call JS from that div

Posted by devzero on Stack Overflow See other posts from Stack Overflow or by devzero
Published on 2010-03-29T10:01:17Z Indexed on 2010/03/29 10:03 UTC
Read the original article Hit count: 293

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX