Javascript Add Class To Body

| | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

The jQuery addClass () method provides a way for developers to modify elements more globally. As the name of the method suggests, we can add a class attribute to a selected element. Why should we do this ?

Let’s take the example of a web page that has existing elements like a title, a paragraph and a click button that does something. Along with this page is a stylesheet that uses these ’ class name ’ elements to style them . By clicking on the button, existing content can change style or appearance.

We could use the addClass () method to insert a new style attribute for the existing content after clicking the button, which allows you to change the style of the existing content using a single line of code. This allows the developer to think about manipulating elements in a stylized way.

Let’s take a look at the example and see how we might code it.

How jQuery addClass () works

In our example, we change the class attribute of an object when our button is clicked. Before we get into this sample code, let’s take a look at how addClass () works. Suppose we have this HTML code:

We use