EyTa
EyTa

Reputation: 109

Control a toggle button through another button

I have a toggle button which is working, i just want to create a button that will only interect with the toggle, when i click the button the toggle will go on, when i click again in the button the toggle will go off.

Toggle.html

<ion-toggle class="toggle-small" toggle-class="toggle-calm" id="toggle" (ionChange)="add(ioToggle.checked)"  #ioToggle ></ion-toggle> 

Is there a way to do it ?

Upvotes: 2

Views: 697

Answers (2)

Ari
Ari

Reputation: 7556

First add [(ngModel)]="status"

<ion-toggle [(ngModel)]="status" class="toggle-small" toggle-class="toggle-calm" id="toggle" (ionChange)="add(ioToggle.checked)"  #ioToggle ></ion-toggle> 

Then add a button:

<!-- Bind the click event to a method -->
<button ion-button (click)="buttonClick($event)">
  Click me!
</button>

in .ts: First define status at the beginning of the class, and then define the buttonClick function:

...
status = false;
...
buttonClick(event) {
    this.status = !this.status 
}

Upvotes: 1

DCR
DCR

Reputation: 15700

function switchx(){
var x = document.getElementById('input1')

x.checked = !x.checked}

/*the stopit function prevents being able to click on the switch */

function stopit(){
var x = document.getElementById('input1')

x.checked = !x.checked


}
#button2{
display:none;
}

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {display:none;}

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

.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: #2196F3;
}

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

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}
<h2>Toggle Switch</h2>

<label class="switch">
  <input id='input1' type="checkbox" onclick='stopit()'>
  <span class="slider"></span>
</label>

<button id='button1' onclick='switchx()'>switch</button>

Upvotes: 0

Related Questions