Checkbox disappearing when click

Asked

Viewed 77 times

0

I’m making a custom checkbox and when I click, it disappears. I want, when I click it, to appear True or False without it disappearing.

When I remove the onClick() event the checkbox does not disappear, but neither does True or False appear.

Here is the code:

<style>
#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;  
  margin-left: 5px;
  margin-top: 5px;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}


/* The container */
.container {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 4px;
  cursor: pointer;
   font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Hide the browser's default checkbox */
.container input {
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

/* Create a custom checkbox */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
  background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
  background-color: #2196F3;
}

/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
  display: block;
}

/* Style the checkmark/indicator */
.container .checkmark:after {
  left: 9px;
  top: 5px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  }
 
 
 
 
 
 input[type=text], select {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

input[type=submit] {
  width: 100%;
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

input[type=submit]:hover {
  background-color: #45a049;
}

table{
			top: inherit;
             width: 310px;
             height: 350px;            
             overflow: auto;
             display: inline-block;
             margin-left: 5px;
             margin-top: 5px;
}
</style>
<body>

<table id="customers">
  <tr>
    <th colspan="2">Tabela</th>
  </tr>
  <tr>
    <td>Boolean</td>
    <td>
	<label class="container" id="lblBool">True</label>
	  <input type="checkbox" checked="checked" id="check" onclick="myCheck()"></input>
	  <span class="checkmark"></span>
</td>
</tr>
<script>
function myCheck() {
  var checkBox = document.getElementById("check");
  if (checkBox.checked == true){
  document.getElementById("lblBool").innerHTML = "False";
  } else {
  document.getElementById("lblBool").innerHTML = "True";
  }
}
</script>
</table>

</body>

  • 1

    Ta very messed up the Html you open a tag label and closes it with a tag input, has td and tr no closures, etc..

  • True, I fixed those details, but still the error remains.

1 answer

3


I made some adjustments and it seems to be working the way it should.

  • HTML was broken (for example, </input> where it should be </label>, <tr> without closing, etc).

  • The function myCheck() was switching text but not checkbox status.

function myCheck() {
  var checkBox = document.getElementById("check");
  if (checkBox.checked == true){
    document.getElementById("check").checked = false;
    document.getElementById("lblBool").innerHTML = "False";
  } else {
    document.getElementById("check").checked = true;
    document.getElementById("lblBool").innerHTML = "True";
  }
}
#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;  
  margin-left: 5px;
  margin-top: 5px;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}


/* The container */
.container {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 4px;
  cursor: pointer;
   font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Hide the browser's default checkbox */
.container input {
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

/* Create a custom checkbox */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
  background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
  background-color: #2196F3;
}

/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
  display: block;
}

/* Style the checkmark/indicator */
.container .checkmark:after {
  left: 9px;
  top: 5px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  }
 
 
 
 
 
 input[type=text], select {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

input[type=submit] {
  width: 100%;
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

input[type=submit]:hover {
  background-color: #45a049;
}

table{
			top: inherit;
             width: 310px;
             height: 350px;            
             overflow: auto;
             display: inline-block;
             margin-left: 5px;
             margin-top: 5px;
}
<table id="customers">
  <tr>
    <th colspan="2">Tabela</th>
  </tr>
  <tr>
    <td>Boolean</td>
    <td>
      <div class="container">
        <label id="lblBool">True</label>
        <input type="checkbox" checked="checked" id="check"  />
        <span class="checkmark" onclick="myCheck();"></span>
      </div>
    </td>
  </tr>
</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.