Question

Proper abstraction aside, how can I grab the 'title' option of a jQuery dialog amongst multiple scripts?

For example, below I have 2 scripts where the first returns a var as a string, where the other does not return the variable as a string:

<script>
$(function() {

    $( ".scheduleDiv" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        Cancel: function() {
            $( this ).dialog( "close" );
        }
    });

    $( ".device-modal" )
    .click(function() {
        $( ".scheduleDiv" ).dialog({"title": $(this).attr('title')});
        alert($( ".scheduleDiv" ).dialog('option', 'title'));
        var single = $(this).dialog('option', 'title');         //works as expected
        $( ".scheduleDiv" ).dialog( "open" );
        return false;
    });
});
</script>

<script>
$(document).ready(function() {
    $('.locks').hide();
    $('.device_groups').hide();

    var single = $(this).dialog('option', 'title');              //is undefined...

    if (single == "Add Single Lock") {
        $('.locks').show();
        //$('.locks select').removeAttr('disabled');
    }
    else {
        $('.device_groups').show();
        //$('.device_groups select').removeAttr('disabled');
    }

});
</script>

So, how can I set 'single' to return the title as a string in the second script?

Many Thanks

Was it helpful?

Solution

Your context of this is likely not correct. When you handle the $(document).ready() event, this is the document. You need to manually grab your dialog window, and then get the title:

$('.scheduleDiv').dialog('option', 'title');

The code you are using to get the title is firing ondocumentready, but your initialization doesn't actually set a title. You are not setting the title until you click the 'deviceModal'. At that point, the code to log the title will not get re-fired, as the document only loads once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top