• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

324
Views
How to check/uncheck checkbox by clicking outside specific block?

So I have a div that appears/disappears on clicking checkbox.


    <input type="checkbox" id="btn">
    <div id="box"></div>

    #box {
        display: none;
        width: 100px;
        height: 100px;
    }

    #btn:checked + #box
        display: block;
    }

But I also want to add an option to close it by clicking anywhere outside this box. How can I do it?

about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Check the below code. You can use label for to achieve this behavior. You click a box outside of checkbox and it gets checked as you wanted.

label {
  display: block;
  margin-top: 100px;
  width: 200px;
  height: 200px;
  background: limegreen;
}

#btn:checked~#box {
  display: block;
}

#box {
  margin-top: 50px;
  display: none;
  width: 200px;
  height: 200px;
  background: red;
}
<input type="checkbox" id="btn">
<label for="btn"></label>

<div id="box">
</div>

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error