Changing the color of a button or div is much easier using css() method in jQuery.You can add extra CSS attributes and properties using jQuery css method.
How to change the color of a button or div using the jQuery library?
To change the color of a div or button, you have to use the ready function of jquery.
like this -
$(document).ready(function() {
$( "#my_btn" ).click(function() {
});
});
Use the jQuery CSS method to change the color of a div or button like this -
$("#my_btn").css({'color': '#6600FF', 'background-color' : '#CC3333'});
or
$("#my_divid").css({'color': '#6600FF', 'background-color' : '#CC3333'});
Let's create an example -
<!DOCTYPE html>
<html>
<head>
<title>Page title </title>
<style type="text/css">
.my_btn{
background: blue;
color: white;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$( "#my_btn" ).click(function() {
$("#my_btn").css({'color': '#6600FF', 'background-color' : '#CC3333'});
});
});
</script>
</head>
<body>
<button type="button" class="my_btn" id="my_btn">My Button</button>
</body>
</html>
As you can see in the above code, I used jQuery css() method to add extra CSS on the button click.
In this way, you can change button or div color using jQuery .