The data-toggle attributes in Twitter Bootstrap
What does data-toggle attributes do in Twitter Bootstrap? I couldn’t find an answer in Bootstrap API.
I have seen a similar question before as well, link.
But it didn’t help me much.
It is a HTML5 data attribute that automatically hooks up the element to the type of widget it is.
data-toggle="modal" data-toggle="collapse" data-toggle="dropdown" data-toggle="tab"
One working example:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <div class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#">Dropdown trigger</a> <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel"> <li><a href="#">Item</a></li> </ul> </div>
Any attribute that starts with
data- is the prefix for custom attributes used for some specific purpose (that purpose depends on the application). It was added as a semantic remedy to people’s heavy use of
rel and other attributes for purposes other than their original intended purposes (
rel was often used to hold data for things like advanced tooltips).
In the case of Bootstrap, I’m not familiar with its inner workings, but judging from the name, I’d guess it’s a hook to allow toggling of the visibility or perhaps a mode of the element it’s attached to (such as the collapsable side bar on Octopress.org).
For example, say you were creating a web application to list and display recipes. You might want your customers to be able to sort the list, display features of the recipes, and so on before they choose the recipe to open. In order to do this, you need to associate things like cooking time, primary ingredient, meal position, and so on right inside the list elements for the recipes.
<li><a href="recipe1.html">Borscht</a></li> <li><a href="recipe2.html">Chocolate Mousse</a></li> <li><a href="recipe3.html">Almond Radiccio Salad</a></li> <li><a href="recipe4.html">Deviled Eggs</a></li>
In order to get that information into the page, you could do many different things. You could add comments to each LI element, you could add rel attributes to the list items, you could place all the recipes in separate folders based on time, meal, and ingredient (i.e. ). The solution that most developers took was to use class attributes to store information about the current element. This has several advantages:
- You can store multiple classes on an element
- The class names can be human readable
- The class is associated with the element it’s on
But there are some major drawbacks to this method:
- You have to remember what the classes do. If you forget or a new developer takes over the project, the classes might be removed or changed without realizing that that affects how the application runs.
- Classes are also used for styling with CSS, and you might duplicate CSS classes with data classes by mistake, ending up with strange styles on your live pages.
All the other methods I suggested had these problems as well as others. But since it was the only way to quickly and easily include data, that’s what we did.
HTML5 Data Attributes to the Rescue
HTML5 added a new type of attribute to any element—the custom data element (data-*). These are custom (denoted by the *) attributes that you can add to your HTML elements to define any type of data you want. They consist of two parts:
This is the name of the attribute. It must be at least one lowercase character and have the prefix data-. For example: data-main-ingredient, data-cooking-time, data-meal. This is the name of your data.
Like any other HTML attribute, you include the data itself in quotes separated by an equal sign. This data can be any string that is valid on a web page. For example: data-main-ingredient=”chocolate”.
You can then apply these data attributes to any HTML element you want. For example, you could define the information in the example list above:
<li data-main-ingredient="beets" data-cooking-time="1 hour" data-meal="dinner"><a href="recipe1.html">Borscht</a></li> <li data-main-ingredient="chocolate" data-cooking-time="30 minutes" data-meal="dessert"><a href="recipe2.html">Chocolate Mousse</a></li> <li data-main-ingredient="radiccio" data-cooking-time="20 minutes" data-meal="dinner"><a href="recipe1.html">Almond Radiccio Salad</a></li> <li data-main-ingredient="eggs" data-cooking-time="15 minutes" data-meal="appetizer"><a href="recipe1.html">Deviled Eggs</a></li>
From the Bootstrap Docs:
So many answers have been given, but they don’t get to the point. Let’s fix this.
To the point
- Any attribute starting with
data-is not parsed by the HTML5 parser.
- Bootstrap uses the
data-toggleattribute to create collapse functionality.
How to use: Only 2 Steps
class="collapse"to the element
#Ayou want to collapse.
data-toggle attribute allows us to create a control to collapse/expand a
div (block) if we use Bootstrap.
The presence of this data-attribute tells Bootstrap to switch between visual or a logical states of another element on user interaction.
It is used to show modals, tab content, tooltips and popover menus as well as setting a pressed-state for a toggle-button. It is used in multiple ways without a clear documentation.