Tabs Widget page
Create a tabs widget using your own version of jQuery.
Overview
In this part, we will:
- Create a
$.fn.tabs
widget.
Slides
Exercise: $.fn.tabs
The problem
Create a progressively enhanced tabs widget. It will be called like:
Notice that .tabs()
can be called on multiple elements. An independent tabs widget should be created
on each element in the collection. The elements in the collection should be <ul>
elements with the
following structure:
The <ul>
elements will have <li>
children which serve as the buttons. Each <li>
must contain an <a>
element. The <a>
element’s href
attributes reference the
id
of a tab element to show when the corresponding <li>
button element is
clicked.
For example when this <li>
is clicked:
The following tab element should be shown:
Finally:
- Each
<ul>
should havetabs
added to itsclassName
. - Each tab element should have
tab
added to itsclassName
.
The following CodePen can be used to complete the exercise.
What you need to know
An Immediately Invoked Function Expression (IFFE) can be used to prevent variables and functions from being added to the global scope:
The following jQuery functions will be useful:
$("#selector")
- Get a collection of elements using a CSS selector.$([element])
- Create a jQuery collection from an array of elements.collection.children()
- Return a collection of all direct descendants of elements in the source collection.collection.find(selector)
- Using a CSS selector, find elements descended from elements in the source collection.collection.addClass(className)
- Add a class name to elements in the collection.collection.removeClass(className)
- Remove elements in the collection.collection.show()
- Show elements in the collection.collection.hide()
- Hide elements in the collection.collection.bind(event, handler)
- Listen to an event.$.each(collection, cb(i, value) )
- Loop through an array-like collection of elements.