Is there any difference between data-dismiss and hide(API) in Twitter BootStrap 3?
We use data-dismiss="modal"
to close the modal.
We also call API $("#modalId").modal("hide")
to close modals.
Both of them can trigger hide.bs.modal
event . But what’s the difference?
3
Answers
so there are two ways to dismiss or hide the modal .
case 1
data-dismiss="modal"
we use
data-dismiss="modal"
if we want to close the modal without doing any activity or its the text modal , user have read it and now he wants to close it.case 2
$(#"modalId").modal("hide")
we call $(#”modalId”).modal(“hide”) using JavaScript this can be a scenario where modal ask the user to input some kind of data and if the data is successful modal should close . for e.g ajax call
in the above example let’s assume you have a modal with form which calls an ajax call. then on success you would like to close that form.
this is how i have used till now.
Functionally they will both achieve the same outcome: closing the dialog.
The advantage of using the
data-dismiss
attribute is that you don’t need any JavaScript code to let your user close the dialog; you can do everything in markup. If you don’t have any other JS code running on your page then the ability to get interaction without adding a JS dependency is pretty great.The advantage of directly calling the API is that it gives you more flexibility: you can (as @amyogiji states) call it after you have done something else (like an AJAX call); you can prevent the user closing the dialog based on some validation rules; you can make something unrelated to the dialog close the dialog without the user needing to interact.
As with most decisions in software, there are use cases for both! Generally I would recommend using the simplest solution (i.e.
data-dismiss
) until you need the extra flexibility.When using multiple models on one page open at the same time on top of each other dismissing the topmost with data-dismiss=”modal” will hide all active models.You can use all Bootstrap plugins purely through the markup API without writing a single line of JavaScript. This is Bootstrap’s first class API and should be your first consideration when using a plugin.