3

I have this code make one button and get some value from server.

then on load ( init view ) I got value = 1 and I need make toggle switch turn on automatically.

the code as below

var value = 1;
.switch {
  position: relative;
  display: inline-block;
  width: 100px;
  height: 34px;
}

.switch input {
  display: none;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ca2222;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #2ab934;
}

input:focus+.slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
  -webkit-transform: translateX(68px);
  -ms-transform: translateX(68px);
  transform: translateX(68px);
}

.on {
  display: none;
}

.on,
.off {
  color: white;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  font-size: 10px;
  font-family: Verdana, sans-serif;
}

input:checked+.slider .on {
  display: block;
}

input:checked+.slider .off {
  display: none;
}
<div align="center" id="bit00_3">
  <label class="switch">
    <input type="checkbox" id="bit00_3" value="off" onclick="toggle(this);">
    <div class="slider round">
      <span class="on">ON</span>
      <span class="off">OFF</span>
    </div>
  </label>
</div>

so I need some javascript to make value 1 in js to toggle button on. programmatically and if 0 is off.

I need use it on init view only

1 Answer 1

6

First, you have 2 ids in the same html page, I have changed the input to checkbox1.

Second, the toggle function did not exist and was not being used, at least in the example, so I removed it.

Third, I removed the value of your input because you were not using it, if you want to get the value, just use document.getElementById("checkbox1").checked or just checkbox1.checked (with checkbox assigned to a var)

Just call checkbox1.checked = {0 for unchecked and 1 for checked} or just checkbox1.checked (with checkbox assigned to a var)

obs: I did not use jquery because I did not know if you are using on this page

  var value = 1;
  //you can put the checkbox in a variable, 
  //this way you do not need to do a javascript query every time you access the value of the checkbox
	var checkbox1 = document.getElementById("checkbox1")
	checkbox1.checked = value
	document.getElementById("checkbox1").addEventListener("change", function(element){
		console.log(checkbox1.checked)
	});
.switch {
        position: relative;
        display: inline-block;
        width: 100px;
        height: 34px;
      }

      .switch input {display:none;}

      .slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #ca2222;
        -webkit-transition: .4s;
        transition: .4s;
      }

      .slider.round {
        border-radius: 34px;
      }

      .slider.round:before {
        border-radius: 50%;
      }

      .slider:before {
        position: absolute;
        content: "";
        height: 26px;
        width: 26px;
        left: 4px;
        bottom: 4px;
        background-color: white;
        -webkit-transition: .4s;
        transition: .4s;
      }

      input:checked + .slider {
        background-color: #2ab934;
      }

      input:focus + .slider {
        box-shadow: 0 0 1px #2196F3;
      }

      input:checked + .slider:before {
        -webkit-transform: translateX(68px);
        -ms-transform: translateX(68px);
        transform: translateX(68px);
      }

      .on
      {
        display: none;
      }

      .on, .off
      {
        color: white;
        position: absolute;
        transform: translate(-50%,-50%);
        top: 50%;
        left: 50%;
        font-size: 10px;
        font-family: Verdana, sans-serif;
      }

      input:checked+ .slider .on
      {display: block;}

      input:checked + .slider .off
      {display: none;}
<div align="center" id="bit00_3">
  <label class="switch">
                <input type="checkbox" id="checkbox1">
                  <div class="slider round">
                    <span class="on">ON</span>
                    <span class="off">OFF</span>
                  </div>
              </label>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

nice that code has worked. thank you for save my day!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.