Javascript Form How to - Change div border on input check








Question

We would like to know how to change div border on input check.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.0.2.js'></script>
<style type='text/css'>
input[type="checkbox"]+label span {
  background-color: #EEE;
  border: 2px solid black;
  display: inline-block;
  padding: 5px;
  cursor: pointer;
}<!--   w  ww . j a  va 2s .co  m-->

input[type="checkbox"] {
  display: none;
}

input[type="checkbox"]:checked+label span {
  background-color: red;
}

.myDiv>img {
  margin-top: 5px;
  width: 300px;
}

.myDiv {
  background-color: #FFF;
  float: left;
  padding-bottom: 10px;
  text-align: center;
  width: 310px;
}

.myDiv>.tamanhos {
  margin-bottom: 20px;
  margin-left: 0;
  margin-top: 20px;
  text-align: center;
  width: 100%;
}

.myDiv:first-of-type {
  margin-left: 13% !important;
}

.comboicone {
  float: left;
  height: 38px;
  line-height: 100%;
  margin: 21% 20px 0;
  top: 50%;
  vertical-align: middle;
  width: 38px;
}

.changeborder {
  border: 5px solid black;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
$('.myDiv input:checkbox').on('click', function (e) {
          if ($(this).is(':checked')) {
             $(".myDiv").addClass("changeborder"); 
    }else{
        $(".myDiv").removeClass("changeborder");  
          }
      });
});
</script>
</head>
<body>
  <div class="myDiv">

    <input type="checkbox" id="combo2" name="cb" /> 
      <label for="combo2"><span>check to see border</span></label>
  </div>
</body>
</html>

The code above is rendered as follows: