Click event is calling multiple times within function

Omkar

I've created code for firing some click events. These events are just like nested with using functions. When I click the button the click event called multiple times.

For Ex. I've one button(b1) which opens a sidebar(already hidden). I've another button(b2) which is in the sidebar. When I open sidebar and click on b2 first time, then it's working fine that is it's calling one time. But when I close sidebar and open again the sidebar by using b1 and click on b2, click event triggered twice. Similarly, it triggers multiple times according to sidebar open using b1. Each time the triggers are increasing on click of b1
Below is my code:

<button class="b1">Show Sidebar</button>
<button class="b3">Hide Sidebar</button>
<div class="sidebar">
    <input type="text" class="demo">
    <button class="b2">Triggers</button>
</div>
<script>
    function getSidebarEvents() {
        $('.b2').click(function(){
            console.log('Triggering Multiple times');      
        });
    }
    $('.b1').click(function() {
       //Do something to open sidebar
       getSidebarEvents(); 
    });
    $('.b3').click(function() {
       //Do something to close sidebar
    });
</script>


I know my function is calling multiple times on every click of b1 and click b2's event is initializing multiple time. But I can't change this structure with big changes because my code is so big now. I need something small change(s) that will help me to fix this.

Nick

You should assign the click event handler outside the function like so. Now if you want to simulate a click from inside that function, just call the click event.

function getSidebarEvents() {
    $('.b2').click();
}
$('.b1').click(function() {
    //Do something to open sidebar
    getSidebarEvents(); 
});
$('.b2').click(function(){
    console.log('Triggering Multiple times');      
});
$('.b3').click(function() {
    //Do something to close sidebar
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Modal popup is not working properly, its calling a function multiple times - click event handler runs several times

From Dev

AddEventListener Event calling multiple times

From Dev

javascript function not calling from code behind when i'm calling another function within same click event

From Dev

jquery click event executes multiple times inside setinterval function

From Dev

Click Event Triggered multiple times

From Dev

click event fires multiple times

From Dev

The event .click fires multiple times

From Dev

addEventListener multiple times in a click event

From Dev

Calling async function multiple times

From Dev

Calling onmouseover function multiple times

From Dev

addEventListener 'click' event not recognizing click and not calling function

From Dev

.click function is executed multiple times on every click

From Dev

Javascript conditional click event running multiple times

From Dev

Click event in for each loop fires multiple times

From Dev

click event fires multiple times issue, how to?

From Dev

Click event listener fires multiple times

From Dev

Event fired multiple times on single click

From Dev

Calling a closure function on an event created within the closure

From Dev

Avoid numbers incrementing multiple times when calling a function multiple times

From Dev

Calling Javascript function in class on Click event

From Dev

Function inside ngIf is called multiple times, after a single click event- Angular2_Typescript

From Dev

Calling the same function multiple times with different arguments?

From Dev

calling function multiple times with new results

From Dev

how to prevent calling a function multiple times in Jquery

From Dev

Calling the same function multiple times in select

From Dev

Avoid calling function multiple times in angular

From Dev

React onSetstate function is calling multiple times

From Dev

Calling function multiple times inside while loop

From Dev

Problems with function, firing click() multiple times

Related Related

  1. 1

    Modal popup is not working properly, its calling a function multiple times - click event handler runs several times

  2. 2

    AddEventListener Event calling multiple times

  3. 3

    javascript function not calling from code behind when i'm calling another function within same click event

  4. 4

    jquery click event executes multiple times inside setinterval function

  5. 5

    Click Event Triggered multiple times

  6. 6

    click event fires multiple times

  7. 7

    The event .click fires multiple times

  8. 8

    addEventListener multiple times in a click event

  9. 9

    Calling async function multiple times

  10. 10

    Calling onmouseover function multiple times

  11. 11

    addEventListener 'click' event not recognizing click and not calling function

  12. 12

    .click function is executed multiple times on every click

  13. 13

    Javascript conditional click event running multiple times

  14. 14

    Click event in for each loop fires multiple times

  15. 15

    click event fires multiple times issue, how to?

  16. 16

    Click event listener fires multiple times

  17. 17

    Event fired multiple times on single click

  18. 18

    Calling a closure function on an event created within the closure

  19. 19

    Avoid numbers incrementing multiple times when calling a function multiple times

  20. 20

    Calling Javascript function in class on Click event

  21. 21

    Function inside ngIf is called multiple times, after a single click event- Angular2_Typescript

  22. 22

    Calling the same function multiple times with different arguments?

  23. 23

    calling function multiple times with new results

  24. 24

    how to prevent calling a function multiple times in Jquery

  25. 25

    Calling the same function multiple times in select

  26. 26

    Avoid calling function multiple times in angular

  27. 27

    React onSetstate function is calling multiple times

  28. 28

    Calling function multiple times inside while loop

  29. 29

    Problems with function, firing click() multiple times

HotTag

Archive