Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: Using data attribute to affix a function

Tags:

jquery

Quick Question

I notice this in bootstrap that it was manage to use the right function by using the data attribute. Like in dropdowns, you can use data-toggle to initiate it. I would like to know how this works. This might save me time to declare each of them in .ready or .load

Thanks.

like image 726
Mr A Avatar asked May 27 '26 17:05

Mr A


1 Answers

It works absolutely the same as any other selector (the code from bootstrap modal):

$('body').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) {
    // show modal
});

This bind means: for all elements under body that fit [data-toggle="modal"] selector handle the click event by specified callback function

like image 55
zerkms Avatar answered May 30 '26 06:05

zerkms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!