Reader small image

You're reading from  Bootstrap 4 Cookbook

Product typeBook
Published inJun 2017
Reading LevelBeginner
PublisherPackt
ISBN-139781785889295
Edition1st Edition
Languages
Right arrow
Author (1)
Ajdin Imsirovic
Ajdin Imsirovic
author image
Ajdin Imsirovic

Ajdin Imsirovic is a full-stack web developer who has published several courses on the subject of web design and web development. He has also authored three books, Bootstrap 4 Cookbook, Elm Web Development, and Vue.js Quick Start Guide. In his fourth book, Vue CLI 3 Quick Start Guide, he introduces readers to the rich ecosystem of frontend tooling and best practices.
Read more about Ajdin Imsirovic

Right arrow

Chapter 7. Make Your Own jQuery Plugins in Bootstrap 4

In this chapter, we will cover the following topics:

  • Making the simplest possible jQuery plugin
  • Making the plugin customizable with the extend() and each() methods
  • Integrating a simple CSS Class Replacement plugin with Bootstrap 4

Introduction


Making jQuery plugins can be a very rewarding learning experience. It is good for understanding both jQuery and the inner workings of JavaScript. With so many jQuery plugins available online, and with so many used to extend Bootstrap's capabilities, it is an invaluable piece of knowledge for any frontend developer. In this chapter, we will look at creating the simplest possible jQuery plugin, then we'll build on the foundations and add more options to tweak and extend our plugins.

Making the simplest possible jQuery plugin


In this recipe, we will create the simplest possible Bootstrap plugin, with just the most basic functionality. The intention of having such a recipe in this book is to clarify the core principles of extending Bootstrap 4 via jQuery plugins as a starting point for more complex code.

Getting ready

To get ready, you can review the completed recipe by running harp server in the chapter7/complete folder, and navigate to localhost:9000/recipe07-01. The result you see should be a simple web page, with a navbar, a heading, and a paragraph.

After previewing the result we are trying to achieve, navigate to the chapter7/start folder and open the _js.ejs, _nav.ejs, _recipe-07-01-js.ejs files in the app/partial folder. Also, one level up, in the root of the app folder, open the file titled recipe07-01.ejs.

How to do it…

  1. Open the currently blank _recipe07-01-js.ejs and paste the code as follows:
// Step 1: Add an IIFE
(function($) {
  // step 2 code goes here
})(jQuery...

Making the plugin customizable with the extend() and each() methods


In this recipe, we will build on the concepts discussed in the preceding recipe. The plugin we make in this recipe will have an additional functionality, using jQuery's extend() method. This will allow us to both specify defaults and to allow plugin users to override those defaults if they need to. This makes our plugin easier to use and allows us to cater for many more use-case scenarios.

Getting ready

To get ready, you can review the completed recipe by running harp server in the chapter7/complete/recipe2 folder and navigating to localhost:9000/recipe07-02. The result you see should be a simple web page with a navbar, a heading, and a paragraph.

After previewing the result we are trying to achieve, navigate to the chapter7/start folder and open the _js.ejs, _nav.ejs, _recipe-07-02-js.ejs files in the app/partial folder. Also, one level up, in the root of the app folder, open the file titled recipe07-02.ejs.

How to do it…

  1. Open...

Integrating a simple CSS Class Replacement plugin with Bootstrap 4


In this recipe, we will build on the concepts discussed in the preceding recipe. The plugin we make in this recipe will be used to help us showcase Flexbox layout options in the next chapter. For now, we will focus on making the plugin itself. The goal of the plugin is very simple--we want to have a plugin that we can pass two classes to. Then, in our HTML, we will add a button, and it will serve as a toggle. On clicking it, it will find the first css class and replace it with the second one. On another click, it will do the opposite.

Getting ready

To get ready, you can review the completed recipe by running harp server in the chapter7/complete folder and navigating to localhost:9000/recipe07-03. To see the simple effect we can achieve with this plugin, click on the button that reads Toggle Navbar Color.

After previewing the result we are trying to achieve, navigate to the chapter7/start folder and open the _js.ejs, _nav.ejs...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Bootstrap 4 Cookbook
Published in: Jun 2017Publisher: PacktISBN-13: 9781785889295
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ajdin Imsirovic

Ajdin Imsirovic is a full-stack web developer who has published several courses on the subject of web design and web development. He has also authored three books, Bootstrap 4 Cookbook, Elm Web Development, and Vue.js Quick Start Guide. In his fourth book, Vue CLI 3 Quick Start Guide, he introduces readers to the rich ecosystem of frontend tooling and best practices.
Read more about Ajdin Imsirovic