Why is .click() and on("click") behaving differently on a bootstrap collapse button?

João Paiva

I'm trying to add a click event to a a bootstrap button that will do some stuff and then flush the event by returning false. This means I don't want the bootstrap effect that happens automatically to happen at all. I will control this programatically inside the click event based on some conditions. This means that I need to flush the event after I'm done with it so that the bootstrap collapse that happens on click will never happen.

My problem is the following. If I use $("my-button").click(...); this catches the event before the collapse has been toggled and I can flush the event after I've done what I want.

However I need to use the on("click", ...); because I need this event to occur with dinamically added buttons.

My problem is that the on("click", ...); doesn't work like the .click(...) and when it catches the event the hidden div has already collapsed which I don't want.

I've made a simple example to demonstrate my problem: https://jsfiddle.net/vo1npqdx/368/.

$(document).ready(function() {
  $("#collapse-button").click(function() {               
    debugger;
    //Do my stuff
    return false; // successfully prevents bootstrap animation.
  });

   $(document).on("click", "#collapse-button", function () {
    debugger;
    //Do my stuff
    return false; // doesn't prevent animation
   });
   // These catch the click event at different times!
});

Is there any possible workaround to this situation?

yezzz

Well, you're in luck, because it looks like bootstrap uses the document to catch the event, meaning you can catch it before that, eg using the body element, but it's better to catch it closer to the element on which the event is triggered, eg. on a wrapper that won't change.

$("#wrapperthatwontchange").on("click", "#collapse-button", function() {
    return false;
  });

https://jsfiddle.net/Lbqpr7pm/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is my anchor behaving differently after I click it the second time?

Bootstrap Navbar collapse button not working on click

How to show a bootstrap collapse div on button click?

Bootstrap collapse all accordions on button click

$(document).on click handler and standard click handler behaving differently on iOS

Multiple bootstrap collapse panel opens at one button click

Collapse button is showing but not working after click in bootstrap 3.3 and jquery 1.1

Bootstrap 5 collapse wont revert on second Button click

Why is there no reaction after I click the collapse toggle button?

Why Bootstrap 3 Collapse is not synchronized with checkbox state on double click?

Expand and collapse Relativelayout by button click

Why is the button in the body behaving differently than the button in my NavigationView toolbar?

Twitter Bootstrap button click to toggle expand/collapse text section above button

Why hide() is behaving differently?

why JavaScript is behaving differently

How to collapse the ExpansionTile on button click? - Flutter

Collapse (close) Menu in SidebarMenu of Shinydashboard on button click

click on an expand & collapse button selenium cucumber eclipse

On click stopPropagation while keeping bootstrap collapse behavior

Simulate a click to open Bootstrap collapse element

Hide Twitter Bootstrap nav collapse on click

Menu content does not collapse on click - Bootstrap

Bootstrap Collapse columns not aligning properly on click

Why would a button click event cause site to reload in a Bootstrap form?

Why at extra small screen size, my collapse button doesn't open when i click on it?

why navbar Bootstrap menu collapse close when click tab panel item

Bootstrap modal not displaying on button click

Bootstrap button tooltip hide on click

Bootstrap Modal Button Click jquery

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive