Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the data-toggle attribute work? (What's its API?)

Bootstrap uses a custom attribute named data-toggle. How does this feature behave? Does using it require Bootstrap's JavaScript library? Also, which data-toggle options are available. So far, I count

  • collapse
  • tab
  • modal
  • dropdown

What do each of these do?

TLDR; What's the API for bootstrap's custom data-toggle attribute?

like image 765
jpaugh Avatar asked Jun 03 '15 20:06

jpaugh


People also ask

What does the data toggle attribute do?

The data-toggle is an HTML-5 data attribute defined in Bootstrap. The advantage of using this is that, you can select a class or an id and hook up the element with a particular widget.

How do you toggle data in Javascript?

Just add data-toggle="collapse" and a data-target to element to automatically assign control of a collapsible element. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element. If you'd like it to default open, add the additional class in.

How does Javascript toggle work?

The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.


1 Answers

I think you are a bit confused on the purpose of custom data attributes. From the w3 spec

Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements.

By itself an attribute of data-toggle=value is basically a key-value pair, in which the key is "data-toggle" and the value is "value".

In the context of Bootstrap, the custom data in the attribute is almost useless without the context that their JavaScript library includes for the data. If you look at the non-minified version of bootstrap.js then you can do a search for "data-toggle" and find how it is being used.

Here is an example of Bootstrap JavaScript code that I copied straight from the file regarding the use of "data-toggle".

  • Button Toggle

    Button.prototype.toggle = function () {   var changed = true   var $parent = this.$element.closest('[data-toggle="buttons"]')    if ($parent.length) {     var $input = this.$element.find('input')     if ($input.prop('type') == 'radio') {       if ($input.prop('checked') && this.$element.hasClass('active')) changed = false       else $parent.find('.active').removeClass('active')     }     if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change')   } else {     this.$element.attr('aria-pressed', !this.$element.hasClass('active'))   }    if (changed) this.$element.toggleClass('active') } 

The context that the code provides shows that Bootstrap is using the data-toggle attribute as a custom query selector to process the particular element.

From what I see these are the data-toggle options:

  • collapse
  • dropdown
  • modal
  • tab
  • pill
  • button(s)

You may want to look at the Bootstrap JavaScript documentation to get more specifics of what each do, but basically the data-toggle attribute toggles the element to active or not.

like image 158
rogergarrison Avatar answered Sep 18 '22 15:09

rogergarrison