Sukhmeet Singh
Sukhmeet Singh

Reputation: 130

Jquery Function running multiple times

I am using two scripts on my page and there is a general click function which records the number of clicks one user is making. So when I click on any element in the document, the click function should run after which other functions on the same element runs. But in my case, the click function runs multiple times before passing the control to the other function.

/************ 1st Jquery Script ***************/
    function($) {
      $(function(e) {
       $('.signupCustom').click(function(){
        var email = $('#form-email').val()
        var password = $('#pass').val()
        var firstName= $('#form-first-name').val()
        var lastName= $('#form-last-name').val()
        var number= $('#form-mobile').val()
        var type=$('#sel1').val()

        $.ajax({
            url: '/login',
            type: 'GET',
            data: {
                'email': email,
                'password':password,
                'firstName':firstName,
                'lastName':lastName,
                'number':number,
                'type':type
            },
            success: function(data){
                if ($('#sel1').val() == "Travel-Agent"){
                    window.location.href = "/agentVerification.html"
                }
                else{
                    window.location.href = "/dashboardTraveller"
                }
            }
        })
    })

    $('.login').click(function(){
        var email = $('#form-username').val()
        var password= $('#form-password').val()
        $.ajax({
            url: '/loginCustom',
            type: 'GET',
            data: {
                'email': email,
                'password':password
            },
            success: function(data){
                window.location.href = data['url']
            }
        })
    })

    $('.destinationsButton').click(function(){

        var url="/destinations";
        window.location=url;
    })


});   })(jQuery);

I have attached the link to the html page which contains the second script.

Link to Page

If you go to this link, there is an image:

enter image description here

When I click on Login button, the click function runs multiple times before control goes to other function. I want the click function to run single time and then control should go to other function.

I tried e.stopPropagation but in case of a popup on same page, the popup does not open. Here on clicking on login, popup comes.

Upvotes: 2

Views: 1258

Answers (2)

Christopher Thomas
Christopher Thomas

Reputation: 372

Is there any reason you are using the below?

jQuery('*').on("click",function(e){});

I think this might work better

jQuery('body').on("click",function(e){});

good luck

Upvotes: 3

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85633

I don't know where you're stuck but you may use one to run the click function just once:

$(selector).one('click',function(){
   //code runs only one click
});

Upvotes: 0

Related Questions