Dim the opacity of a div by selecting a checkbox

Is there a way to gray out or disable a specific div when a checkbox is checked? I could use some guidance with an example on JSfiddle perhaps?

Here is the code snippet: http://jsfiddle.net/albertoc/vFrAB/5/

HTML:

<div id="wrap" style="width:500px">
    <div id="content">
        <p>This particular box will be disabled! <a href="#">Link</a>
        </p>
    </div>
    <div id="darkLayer" class="darkClass" style="display:none"></div>
    <div id="checkbox">
        <form>
            <input type="checkbox" id="myCheckBox" />
        </form>
    </div>
</div>

CSS:

.darkClass
{
    background-color: white;
    filter:alpha(opacity=50); /* IE */
    opacity: 0.5; /* Safari, Opera */
    -moz-opacity:0.50; /* FireFox */
    z-index: 20;
    height: 100%;
    width: 100%;
    background-repeat:no-repeat;
    background-position:center;
    position:absolute;
    top: 0px;
    left: 0px;
}

JS:

function dimOff()
{
    document.getElementById("darkLayer").style.display = "none";
}
function dimOn()
{
    document.getElementById("darkLayer").style.display = "";
}

Answer №1

I have streamlined your code by merging two functions and adjusting the z-index of your checkbox so that it can still be clicked even with an overlay.

function disableCheckBox(checkbox) {
    document.getElementById("darkLayer").style.display = (checkbox.checked) ? '' : 'none';
}

Check out the modified jsFiddle example here

CSS

#checkbox {
    position:relative;
    z-index:21;
}

HTML

<div id="wrap" style="width:500px">
    <div id="content">
        <p>This disabled box contains a <a href="#">Link</a></p>
    </div>
    <div id="darkLayer" class="darkClass" style="display:none"></div>
    <div id="checkbox">
        <form>
            <input type="checkbox" id="myCheckBox" onchange="disableCheckBox(this)" />
        </form>
    </div>
</div>

Answer №2

Relocate the .darkLayer within the #content

<div id="wrap" style="width:500px">
    <div id="content">  
        <div id="darkLayer" class="darkClass" style="display:none"></div>
        <p>This box will be disabled! <a href="#">Link</a></p>
    </div>
    <div id="checkbox">  
        <form>
            <input type="checkbox" id="myCheckBox" />
        </form>
    </div>
</div>

Add styling to #content so it functions as an offsetParent/scrollParent.

#content {
    position:relative;
}

Set up a listener for the checkbox

var i = 0;
document.getElementById("myCheckBox").addEventListener('click', function () {
   [dimOff, dimOn][i=1-i](); 
});

Now the .darkLayer will only cover the #content and not the second <div> containing the checkbox.

DEMO

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I eliminate the blending effect between different layers?

Experimenting with blend modes has led me to create a composite image featuring a shirt and a pocket. Initially, everything seemed fine when I only had the shirtVer and shirtHor layers. I could easily merge them and adjust the colors of the checkered patte ...

How to send a JSON object using Node.js Express 4.0

I'm currently working on parsing the JSON object sent in the request and displaying the data being transmitted. Below is my post request: $.ajax({ url: url, type: 'POST', contentType: 'application/json' ...

Obtain the initial value and update the information without having to refresh the page using Angular Select

One issue I am facing is to retrieve the default value in the select dropdown Another problem arises when I select an option from the first dropdown menu, the data appears in the second dropdown. However, upon changing the selection in the first dropdown ...

What is the solution for the error message "TypeError: app.use() is seeking a middleware function"?

I am a beginner in Node.js and have encountered an issue in my passport.js or signupLogin.js file with the error message, app.use() requires a middleware function that I am struggling to resolve. I suspect it may be related to the signupLogin route, as th ...

Creating a React functional component that updates state when a specific window breakpoint is reached

Having an issue with my code when it hits the 960px breakpoint. Instead of triggering once, it's firing multiple times causing unexpected behavior. Can someone help me troubleshoot this problem? const mediaQuery = '(max-width: 960px)'; con ...

How can you transfer the selected options value from a select menu to a button's onclick event in

Is it possible to pass the value of select-options, but then change the approach and pass the value first on the button for onClick function? Thank you! import { useSelector, useDispatch } from "react-redux"; const Purchase = () => { const products ...

Tips for retrieving the value of a dynamic and multi-update id using jQuery and Ajax

I'm in need of assistance with a few different tasks: Firstly, I am looking to understand how to retrieve the id of an input element when it is dynamically generated from a database. In the HTML, it displays something like field 1 -> id = "pa ...

What steps should I take to prevent my <mat-card> from expanding whenever messages are shown in Angular 9?

I have a <mat-card> containing a login form on my page. However, when error messages are displayed, the vertical size of the <mat-card> changes and I need it to remain the same. Is there a way to prevent this from happening? Below, you will ...

Encountering an error when setting up a React-TypeScript ContextAPI

I am currently attempting to understand and replicate the functionality of a specific package found at: https://github.com/AlexSegen/react-shopping-cart Working within a React-Typescript project, I have encountered challenges when creating the ProductCont ...

How to access a grandchild's property using a string in JavaScript

Is there a way to access a property nested deep within an object when creating a custom sorting function? I am attempting to implement a sort function that can sort an array based on a specific criteria. const data = [ { a: { b: { c: 2 } ...

Passing a JSON object to a different page using jQuery

I'm currently facing a challenge with sending JSON data (demographics) to a new page within the same directory when a user clicks on a marker that I've placed on a Google map on my website. I am using the jquery-ui-map plugin, and while the marke ...

Utilizing AJAX to input information into the database with Magento

I'm just starting to learn about ajax and I may be approaching this problem incorrectly. Essentially, I have a javascript variable that needs to be added to the database. Here is what I have so far... onInit: function() { window.fcWidget.on(&apos ...

There seems to be a malfunction with the hide and reset features, as they are

I have encountered an issue while working on hiding a series in Google Charts when clicked on the legend. The problem arises when an extra column is added to display tooltips on the bars, causing the functionality to break. Please check out the demos below ...

What is the best way to access specific information about the top card in a Trello list?

Is there a way for me to retrieve the name, label, and due date of the top cards in a Trello list on my board and then send it through Discord using my bot? I would greatly appreciate any guidance on how to achieve this. ...

"Exploring the process of loading various page sections simultaneously with jQuery's ajax function

I have a webpage that displays multiple tables for different categories, each loaded via ajax. The number of categories can vary, but I know the total count before making the ajax calls. I display them all simultaneously using the following approach: $(f ...

When invoking the jQuery ".load('pageName')" method, HTML pages are not loaded from the application cache

I have been working on incorporating the html5 offline caching functionality into our html pages, and everything seems to be running smoothly with jQuery version 1.4. However, I encountered a problem when I upgraded to jQuery 1.8. Specifically, issues aro ...

Creating a Full-Width Dropdown in Bootstrap 4 Navbar: A Step-by-Step Guide

I'm attempting to create a dropdown that spans the full width of the screen and has a collapsible feature like many modern websites. However, I am having difficulty figuring out how to achieve this. I experimented with using Bootstrap's collapse ...

Utilize express.router() to send a get request to a third-party API while including an API key

As I develop my react application, I am faced with the task of retrieving data from a third-party site that requires me to include an API key in the header as 'X-Auth-Token'. Currently, I am using the fetch() API from the client-side JavaScript ...

Angular encountering issue with HTTP service not functioning as expected

I have been encountering issues while trying to retrieve data using JSONPlaceholder in my service. Despite my efforts, I have not been successful in fetching any data. I would greatly appreciate any assistance in resolving this matter. user.component.html ...

Transfer a single search result to a different webpage from a list of multiple results

I am working on a webpage called userLanding.jsp. When a user searches, the page displays multiple results. I have successfully checked the selected dynamic result, but now I need to find a way to transfer the data from the selected div to another page. D ...