Search code examples
javascriptjqueryjquery-tools

Calling jQuery (modal popup) function from within another JavaScript function


I'm trying to call a facebox modal box (flow player's jQuery Tools).
I have a select menu that onChange is calling my sub_debt script. The sub_debt script evaluates the value of the select menu - if the value == 1 (for 'yes'), I would like to call the facebox modal defined in the header.

Currently, the below code is invoking the facebox modal "somewhat" - it is taking the select menu "out" of its place on the page as if it is the only part of the facebox-modal popup itself and applying the darkened background mask to the rest of the page.

(note: the facebox modal script does work properly when called from an anchor tag for example)

    <head>

    <script src="http://cdn.jquerytools.org/1.2.7/full/jquery.tools.min.js"></script>

    <!-- Popups : Facebox -->
    <script>
    function popup(choosebox) {
            var $choosebox = $("#" + choosebox);

            if ($choosebox.hasClass("init")) {
                    $choosebox.overlay().load();
            }
            else {
                    $choosebox.addClass("init");
                    $choosebox.overlay({

             // custom top position
             top: 260,

             mask: { color: '#838383',
                     loadSpeed: 200,
                     opacity: 0.5
             },  
             closeOnClick: true,
             load: true
            });
    }
    }
    </script>

    </head>
    <body>


    <script type="text/javascript">
    function subdebt(choosebox){
        var myField = document.getElementById('subordinate_debt');
        if(myField.value == "1") {
            popup(choosebox); // Calls above function from defined in header
        } else {
            alert("Do not fire popup - you entered: " + myField.value);
        }   
    }
    </script>


    <select name="subordinate_debt" id="subordinate_debt" onchange='subdebt("subordinate_debt");'>
        <option value="" selected="selected"></option>
        <option value="0">No</option>
        <option value="1">Yes</option>
    </select>

Solution

  • The following changes worked. (Thanks "Amit Soni" - oDesk.com contractor)

    <script type="text/javascript">
    function subdebt(choosebox){
        var myField = document.getElementById('subordinate_debt');
    if(myField.value == "1") {
        popup("subordinate_debt_Popup");
    } else {
    alert("Do not fire popup - you entered: " + myField.value);
    }   
    }
    </script>
    

    And changed the "subordinate_debt" DIV ID to "subordinate_debt_Popup"

    <div id="subordinate_debt_Popup" class="facebox">.....</div>