1
votes

In this, I do not get a status of checkbox is it true or false I try my best I did not get any solution. when click on toggle it always gives me a false value

$('input').on("change", function() {
  if ($('input[type="checkbox"].is(":checked")')) {
    alert("its checked");
  }
});
input[type=checkbox] {
  width: 0;
  height: 0;
  visibility: hidden;
}

label {
  cursor: pointer;
  text-indent: -9999px;
  width: 50px;
  height: 20px;
  background: grey;
  display: block;
  border-radius: 100px;
  position: relative;
}

label:after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 90px;
  transition: 0.3s;
}

input:checked+label {
  background: #bada55;
}

input:checked+label:after {
  left: calc(100% - 5px);
  transform: translateX(-100%);
}

label:active:after {
  width: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="toggle" id="switch">
<label for="switch">Toggle</label>
2
I made a snippet and immediately could see that This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting - please next time create a snippet of look in the console before asking - mplungjan
I think you need if ($(this).is(":checked") ) { - Satpal

2 Answers

2
votes

Problem is in your selector. You need to change the position of the ' in your code..!

$('input').on("change", function() {
    if ($('input[type="checkbox"]').is(":checked")) { //or you can use $(this) instead of $(input[type="checkbox"]) to select itself
    alert("its checked");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="toggle" id="switch">
<label for="switch">Toggle</label>
0
votes

Use $(this).is(":checked") instead of.

$('input').on("change", function () {
    if ($(this).is(":checked")) {
        alert("its checked");
    }
});
input[type=checkbox] {
    width: 0;
    height: 0;
    visibility: hidden;
}

label {
    cursor: pointer;
    text-indent: -9999px;
    width: 50px;
    height: 20px;
    background: grey;
    display: block;
    border-radius: 100px;
    position: relative;
}

label:after {
    content: '';
    position: absolute;
    top: 3px;
    left: 3px;
    width: 14px;
    height: 14px;
    background: #fff;
    border-radius: 90px;
    transition: 0.3s;
}

input:checked + label {
    background: #bada55;
}

input:checked + label:after {
    left: calc(100% - 5px);
    transform: translateX(-100%);
}

label:active:after {
    width: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="toggle" id="switch">
   <label for="switch">Toggle</label>

And one thing, you don't need to set width or height to zero, just use visibility

input[type=checkbox] {
    /* width: 0; */
    /* height: 0; */
    visibility: hidden;
}