Search code examples
jqueryfunctionqtip2

Execute a function into another function


I would like to initiate a function by including it in another. In this case, I would like to initiate the function print_help_A_0() during the Check_Index() function is being executed :

$(function () {
    $(document).on('click', '#Demeler', Check_Index);
    $(document).on('click', '#Hasard', Check_Index);

    function Check_Index() {

        var choixA = $('#ChoixA').val().toUpperCase();
        var choixA_l = choixA.length;
        var choixB = $('#ChoixB').val().toUpperCase();
        var choixB_l = choixB.length;

        if(choixA_l == 0 & choixB_l == 0){$('#ChoixA').focus();}
        [...]
        else if(choixA !=  choixB ){
            var id = $(this).attr('id');
            if(id == "Demeler"){
                //run my function print_help_A_0()
            }else if(id ="Hasard"){[...]}
        }
    };

    function print_help_A_0() {         
        var bulle_help_A_0 = <?php echo json_encode(get_option('bulle_help_A_0')); ?>;
        var bulle_misska = $('#misska').qtip({
                content: {text: bulle_help_A_0+'<input type="button" value="go !" id="print_help_A_1"/>'},
                style: {classes: 'qtip-light'},
                position: {my: 'right center',at: 'center left'},
        show: 'none',hide:'none'
    });
    var api_bulle_misska = bulle_misska.qtip('api');
    api_bulle_misska.show();
    };       
});

when I write [...], it's just to simplified my code.

Any idea ? Because, $(document).print_help_A_0(); doesn't work...


Solution

  • You $(document).function doesn't work because you declare your functions inside $(function(){}), declare at document level, put only initialization code inside $(function(){}), Ex:

    function Check_Index() {
    
        var choixA = $('#ChoixA').val().toUpperCase();
        var choixA_l = choixA.length;
        var choixB = $('#ChoixB').val().toUpperCase();
        var choixB_l = choixB.length;
    
        if(choixA_l == 0 & choixB_l == 0){$('#ChoixA').focus();}
        [...]
        else if(choixA !=  choixB ){
            var id = $(this).attr('id');
            if(id == "Demeler"){
                print_help_A_0()
            }else if(id ="Hasard"){[...]}
        }
    };
    
    function print_help_A_0() {         
        var bulle_help_A_0 = <?php echo json_encode(get_option('bulle_help_A_0')); ?>;
        var bulle_misska = $('#misska').qtip({
                content: {text: bulle_help_A_0+'<input type="button" value="go !" id="print_help_A_1"/>'},
                style: {classes: 'qtip-light'},
                position: {my: 'right center',at: 'center left'},
                show: 'none',hide:'none'
        });
    
        var api_bulle_misska = bulle_misska.qtip('api');
        api_bulle_misska.show(); 
    };
    
    $(function () {
        $(document).on('click', '#Demeler', Check_Index);
        $(document).on('click', '#Hasard', Check_Index);      
    });