How to apply styles on an element using jQuery ?
Last Updated :
21 Mar, 2022
In this article, we will learn how one can apply a style on an element using jQuery. Suppose you are building a website or game where you need the user interface to be interactive. In such cases styles can play a huge role there in this post we are going to learn how can we change styles when certain events got triggered with the help of JQuery. We can apply styles on elements by using either the JQuery CSS Classes or the JQuery .css() method.
Approach 1: Using JQuery CSS Classes. This approaches consists of using methods such as the addClass() method, removeClass() method and the toggleClass() method. These methods works similar to the classList.addClass() method and classList.removeClass() method. We can create some classes with some css rules and add or remove them using JQuery as per the requirement.
Syntax:
$("#poll").removeClass("safe").addClass("danger");
// OR
$("#poll").toggleClass("danger");
Example:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<script src=
"https://code.jquery.com/jquery-3.6.0.min.js">
</script>
<style>
#poll {
position: absolute;
bottom: 15%;
}
#submit {
position: absolute;
bottom: 10%;
}
.safe {
background-color: rgb(6, 161, 6);
width: 20%;
height: 30%;
}
.danger {
background-color: rgb(226, 19, 19);
width: 20%;
height: 60%;
}
</style>
</head>
<body>
<h1 style="color: green">GeeksforGeeks</h1>
<h3>How can you apply a style on an
element using jQuery?</h3>
<div id="poll" class="safe"></div>
<button id="submit">Alarm</button>
<script>
$("#submit").click(function () {
// We can either remove the old
// class and add a new one
// $("#poll").removeClass("safe").addClass("danger");
// Otherwise, we can toggle the class
$("#poll").toggleClass("danger");
})
</script>
</body>
</html>
Output:
In the above example, we maintain two different classes safe and dangerous for different states. Initially, class safe is attached to div with id #poll and on clicking the button this safe class is removed and danger class is added dynamically which increased the height of the div and changed its background color to red.
If you are confused about which class to remove and which class to add then the toggleClass("className") method is there for you. This method adds class(which is passed as an argument inside the method) to the HTML tag if not currently added or removes the class from the element if it's currently added to the HTML tag. In simple words, this method toggles between adding/removing classes from the selected elements.
Approach 2: Using the JQuery css() method: In this method, instead of creating separate classes inside the stylesheet, you can directly set styles from javascript itself. The css() method can be used to either return a single property based on the given property or set a single or multiple CSS properties.
Syntax:
// Assigning a single CSS property
$("#phone").css("background", "red");
// Assigning multiple properties
$("#phone").css({"background": "red", "font-size": "16px"});
Example 2:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<script src=
"https://code.jquery.com/jquery-3.6.0.min.js">
</script>
</head>
<body>
<h1 style="color: green">GeeksforGeeks</h1>
<h3>How can you apply a style on an
element using jQuery?</h3>
<b>Telephone Number Verification</b>
<br><br>
<input type="number" id="phone"
placeholder="Enter the Phone No." />
<span id="warning"></span><br><br>
<button id="submit">Check</button>
<script>
$("#submit").click(function () {
// Regular expression to check
// for an indian phone number
const regexExp = /^[6-9]\d{9}$/gi;
const phone = $("#phone").val();
// Add or remove classes based on
// the regular expression check
if (!regexExp.test(phone)) {
$("#warning").text("It's wrong try again!");
$("#warning").css("color", "red");
$("#phone").css("background", "red");
} else {
$("#warning").text("It's correct");
$("#warning").css("color", "green");
$("#phone").css("background", "green");
}
});
</script>
</body>
</html>
Output: We have checked whether the entered Indian phone number is correct or not for which we checked input value against a custom regex pattern if it's false then using the JQuery CSS method we changed the background color of the input Textarea and displayed a warning.
Similar Reads
How to apply CSS style using jQuery ? In this article, we will explore how we can apply CSS styles to HTML elements. It is recommended that you have some basic knowledge of HTML, CSS, JavaScript, and jQuery before beginning this article. It is possible to change the CSS property of an element by using a simple JavaScript API, but we try
2 min read
How to add `style=display:âblockâ` to an element using jQuery? To add the inline style style="display: block" to HTML elements using jQuery, there are several approaches, each with its advantages. Below are the different methods you can use:Table of ContentUsing the CSS() methodUsing the show() methodUsing the attr() methodUsing the prop() methodUsing the CSS()
3 min read
How to change style of elements on scroll using jQuery ? We have given an HTML document containing some CSS properties, and the task is to change the CSS property to a particular element after scrolling the page using jQuery. To change the style of element on scroll, we get the number of pixels by which the content of an element has been scrolled horizont
2 min read
How to apply css property to a child element using JQuery? The task is to apply CSS property to a child element using jQuery. To do that, first we select the child element with the help of children() method in jQuery and then apply CSS property to it with the help of css() method in jQuery. Syntax: // Note : children method selects the direct child to paren
2 min read
How to change the element id using jQuery ? The jQuery methods are used to change the element ID which are described below: jQuery attr() Method: This method set/return attributes and values of the selected elements. If this method is used to return the attribute value, it returns the value of first selected element. If this method is used to
3 min read