You're reading from Bootstrap 4 Cookbook
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.
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.
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
.
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.
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
.
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.
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...