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

The Programming Expert

Solving All of Your Programming Headaches

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

JavaScript Opacity – How to Change the Opacity of an Element Using JavaScript

February 27, 2022 Leave a Comment

In JavaScript, we can change the opacity of a div simply by using the CSS opacity property along with the getElementById() method.

document.getElementById("div1").style.opacity = .2;

Let’s say we have the following HTML:

<style>#div1 { background: green; }</style>
<div id="div1">
  <p>This paragraph is in a div that we want to change the opacity of.</p>
</div>

If we want to change the opacity of #div1 to be semi-transparent, we will use the opacity property in the following JavaScript code.

document.getElementById("div1").style.opacity = .5;

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

To change the opacity of a div using JavaScript, we can target the CSS opacity property with an onclick event.

Let’s say we have the following HTML and we want to change the opacity of #div2 to .2.

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

We can utilize both the opacity property of #div2 along with an onclick event to change the opacity.

Below is the JavaScript code which will allow the user to be able to change the opacity of #div2:

function changeOpacity() {
  document.getElementById("div2").style.opacity = .2;
}

The final code and output for this example of how to change the opacity of our div using JavaScript 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" onclick="changeOpacity()">Change opacity</div>
</div>

<script>

function changeOpacity() {
  document.getElementById("div2").style.opacity = .2;
}

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to change the opacity of an element.

Other Articles You'll Also Like:

  • 1.  parseInt() JavaScript – Convert String to Integer with parseInt() method
  • 2.  Using JavaScript to Add Item to Array if it Does Not Exist
  • 3.  Using JavaScript to Get the Base URL
  • 4.  Using Javascript to Remove Class from Element
  • 5.  Using JavaScript to Get the Length of Array
  • 6.  Using JavaScript to Get Radio Button Value
  • 7.  React Axios Interceptor to Prevent Infinite Loops in JWT Authentication
  • 8.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 9.  How to Remove Decimals of a Number in JavaScript
  • 10.  Using JavaScript to Show and Hide a Div

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