3

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.

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

8 Comments

if the name of my toggle is not modal, does the fomrat for the event changes? like click.myfunc.data-api ? Also, should I put this on my .ready?
@Mr A: [data-toggle="modal"] -- this is a generic selector, means "all elements that have parameter data-toggle equal to modal". So you can use everything as a parameter name and everything for parameter value
click.modal.data-api - is just a click event
Yep, you need to put it in $(function() {});
Can I just go with a click event and or should I be specific with click.modal.data-api?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.