February 25, 2013

simple alert box doesn't close after the first click, what to do to make it work?

Question by sasori

i have a simple jquery script that calls a fancybox after a button was clicked,
now the problem is, the fancy box doesn’t close after the first click of the button, only after the 2nd click…

The second problem is, inside the fancy box, there is a form with 3 input fields, the submit button/button doesn’t work in one click, it needs to get clicked twice as well..any ideas on what to do?

jQuery("#hsbcloanbutton").click(function(e){

    e.preventDefault();
    var loanAmount = jQuery('#loanAmount').val();
    var loanTenure = jQuery('#loanTenure').val();
    var InstalmentAmount = jQuery('#InstalmentAmount').val();
    var loanName = jQuery('#loanName').val();
    var loanNric = jQuery('#loanNric').val();
    var loanContact = jQuery('#loanContact').val();
    if(loanAmount.length < 1 || loanTenure.length  < 1 || InstalmentAmount.length < 1)
    {
        alert("Please enter data");
        return false;
    } else if(isNaN(loanAmount) || isNaN(InstalmentAmount)){
        alert("Please put numbers only in Loan Amount and Instalment Amount fields");
        return false;
    } else {
                jQuery("#hsbcloanbutton").fancybox({
                'titlePosition' : 'inside',
                'transitionIn' : 'none',
                'transitionOut' : 'none',
                'hideOnOverlayClick' : false,
                'hideOnContentClick' : false,
                'showCloseButton' : false,
        });


    }
    jQuery("#hsbcloansubmitformbutton").click(function(e){
        e.preventDefault();
        jQuery.ajax({
            'type' : 'POST',
            'url' : "<?php echo Yii::app()->createUrl?>",   
        )};
    });
});

Answer by Starx

It seems you are initializing your fancybox, on the click event: That might be causing this.

Initialize the script on $(document).ready()

$(function() {
        jQuery("#hsbcloanbutton").fancybox({
                'titlePosition' : 'inside',
                'transitionIn' : 'none',
                'transitionOut' : 'none',
                'hideOnOverlayClick' : false,
                'hideOnContentClick' : false,
                'showCloseButton' : false,
        });
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!