• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / jQuery / jQuery Opacity – How to Change the Opacity of an Element

jQuery Opacity – How to Change the Opacity of an Element

February 27, 2022 Leave a Comment

There are many ways to use jQuery to change the opacity of an element. We can do this by using the jQuery fadeTo() method or the jQuery css() method.

Here is an example using the jQuery fadeTo() method:

$("#div1").fadeTo(1000, .5);

Here is an example using the jQuery css() method:

$("#div1").css("opacity", ".5");

The main difference between these two methods is that the fadeTo() method will change the opacity over a given duration(400 milliseconds is the default).

If you changed the duration to 0 milliseconds, the fadeTo() method should work the same as the css() method. The jQuery css() method will target the opacity property of an element. Moving forward, we will use the jQuery css() method to change the opacity of an element.


Let’s say we have the following HTML:

<style>#div1 { background: green; }</style>
<div id="div1">
  <p>This is some div</p>
</div>

If we want to change the opacity of #div1, we can use the jQuery css() method to target the opacity of the div.

$("#div1").css("opacity", ".2");

The above code would change the opacity of #div1 to .2.

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1").css("opacity", ".2");

Let’s see this in action in the example below.

Using jQuery to Change the Opacity of a Div with a Click

In this simple example, we will have a div with a greenish background. We will provide a button for the user to use to change the div’s opacity.

<style>#div2 { width: 300px; height: 200px; background: #7bbfa2; }</style>
<div id="div1">
  <div id="div2"></div>
  <div id="click-me">Change opacity</div>
</div>

We can utilize both the jQuery click() method and jQuery css() method to change the opacity.

Below is the JavaScript code which will allow the user to change the opacity of the div. We will change the div to have an opacity of .2(barely visible):

$("#click-me").click(function(){
  $("#div2").css("opacity", ".2");
});

The final code and output for this example of using jQuery to change the opacity of a div is below:

Code Output:

Change opacity

Full Code:

<style>#div2 { width: 300px; height: 200px; background: #7bbfa2; }</style>
<div id="div1">
  <div id="div2"></div>
  <div id="click-me">Change opacity</div>
</div>

<script>

$("#click-me").click(function(){
  $("#div2").css("opacity", ".2");
});

</script>

Hopefully this article has been useful for you to understand how to use the jQuery css() method to change the opacity of an element.

Other Articles You'll Also Like:

  • 1.  jQuery append – Insert Element at End of Another Element
  • 2.  Using jQuery to Get Element by ID
  • 3.  jQuery mouseenter – An Interactive Example of the mouseenter Method
  • 4.  Using jQuery to Show a Div
  • 5.  How to Use jQuery to Check if an Element is Visible
  • 6.  Using jQuery to Scroll to Div
  • 7.  Get Padding of an Element Using jQuery
  • 8.  Using jQuery to Remove Class from an HTML Element
  • 9.  Using jQuery to Change Div Text
  • 10.  Changing the Background Image of a div Using jQuery

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy