How to set the opacity level for an element with JavaScript?

JavascriptWeb DevelopmentFront End Scripts

Use the opacity property in JavaScript to set the opacity level. You can try to run the following code to set the opacity level for an element with JavaScript −

Example

Live Demo

<!DOCTYPE html>
<html>
<style>
#box {
width: 450px;
background-color: gray;
}
</style>
<body>
<p>Click below to set Opacity.</p>
<button type="button" onclick="display()">Set Opacity</button>
<div id="box">
<p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
<p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
<p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
</div>
<br>
<script>
function display() {
document.getElementById("box").style.opacity = "0.5";
}
</script>
</body>
</html>
Published on 27-Feb-2018 14:05:13