Skip to main content

Seven Most Common jQuery Methods Last Part: ADD/REMOVE/TOGGLE CLASS()


Add, remove and toggleClass is also a very common way to manipulate style.

.addClass() and .removeClass()

I add two class selector in the style tag. Class one is to change text color to be green, and class two is change both text and background color.



 Now I refresh the page and change h1 text to green by using addClass() method: $("h1").addClass("one").  If you want to remove the style, you can change it to removeClass("one").



I do the same thing to the list. I change the list color and add a background color by adding the class two style value: $("li").addClass("two").


.toggleClass()

 According to jQuery, toggerClass method can add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the state argument.

I only want to change the first list item color and background by writing: $("li").first().toggleClass("two"), so the "Apple" changes to red with a yellow background.


Then, I want to change all the list items color and backgound: $("li").toggleClass("two"). 

As the result, the "Apple" style disappear, and the rest two fruits change the style. That's how toggle works: the "Apple" already had that class two style, so it removed automatically when I set all list items to the same class two style.


Comments

Popular posts from this blog

GIT Study Note: Six Git Commands You Should Know!

What is Git? According to Git website , Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Basically, we can use Git to track all of changes and revert them back if we made mistakes. The version control system is a way for us to work with different versions of our code, so that we can save different features and make notes as we progress. It is also very useful for team collaboration. Six Git Command You Should Know 1. Git Init In our server, we need to create a "Git" directory and inside of "Git", we make another direcory "git_inro". Then, we add a file "app,js" in the "git_intro" folder. The first thing we need to run is "git init" . When we run git init, it will make a directory which is hidden called ".git". Wherever I run that git init command, it's going to track all of our changes. ...

Intermediate Express.Js: Write Conditions and Loops In The EJS (study note)

We continue to use the app.js which we have build to be the study example. In the last post, we only add a simple EJS tag <%= thingVar %> inside h1 tag. This will match the "thing" to any "thing" request from the user. If Statement Today, the course shows us to write a simple if statement in the EJS file. I learn to apply it to my own app. In the if statement, if users send"/wantto/workout" request, the page will show a new content "Good Idea!". We wrote the if statement in the "diet.ejs" file, and we add "<% %>" tags around the code. We have to wrap every line of JavaScript anytime JavaScript starts and ends. Moreover, you may see there are two types of ejs tags here: <%= %> The one with equal sign. According to the course, when we add equal sign, the value that is returned inside of the tag will be rendered to the HTML page. it will be added to the HTML. For example, if we write ...

Express.js Study Note: Route Order Is Matters!

Last post, I have learned to develop my first Express App from the course. Basically,  it has three routes. When we type "/", "/bye" and "/cat", the app will receive the request and send the matched response to us. But, if we want to send a nice message when users type other texts after slash, what should we do? We can write a "*" route which will run whenever out app gets any request to any url aside from those three routes that we have already defined. app.get("*", function) In the App.js file, we add a new star route, and it will send you a new message whenever you type any undefined request. I run my app, and put some random texts after slash, and hit enter. All I get is "You are a star!". If we move the star route to the top of those three routes, what will happen? The instructor run the app, the result is even though we send the "/", "/bye" or "/cat" request, all we get...