0

I"m using a toggle switch to replace my select and 2 options code...

The old code had select with name and two options with values of "0" and "1".

How can i define the values of the on \ off buttons in this new code?

For example i want the value of "OFF" to be 0 and "ON" to be 1.

I"m not using ajax, just a regular form.

This is the Toggle Switch Code:

html,
body {
  display: flex;
  min-height: 100%;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-family: sans-serif;
}

.tgl {
  display: none;
}
.tgl, .tgl:after, .tgl:before, .tgl *, .tgl *:after, .tgl *:before, .tgl + .tgl-btn {
  box-sizing: border-box;
}
.tgl::-moz-selection, .tgl:after::-moz-selection, .tgl:before::-moz-selection, .tgl *::-moz-selection, .tgl *:after::-moz-selection, .tgl *:before::-moz-selection, .tgl + .tgl-btn::-moz-selection {
  background: none;
}
.tgl::selection, .tgl:after::selection, .tgl:before::selection, .tgl *::selection, .tgl *:after::selection, .tgl *:before::selection, .tgl + .tgl-btn::selection {
  background: none;
}
.tgl + .tgl-btn {
  outline: 0;
  display: block;
  width: 4em;
  height: 2em;
  position: relative;
  cursor: pointer;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
}
.tgl + .tgl-btn:after, .tgl + .tgl-btn:before {
  position: relative;
  display: block;
  content: "";
  width: 50%;
  height: 100%;
}
.tgl + .tgl-btn:after {
  right: -50%;
}
.tgl + .tgl-btn:before {
  display: none;
}
.tgl:checked + .tgl-btn:after {
  right: 50%;
}

.tgl-flat + .tgl-btn {
  padding: 2px;
  transition: all .2s ease;
  background: #fff;
  border: 4px solid #f2f2f2;
  border-radius: 2em;
}
.tgl-flat + .tgl-btn:after {
  transition: all .2s ease;
  background: #f2f2f2;
  content: "";
  border-radius: 1em;
}
.tgl-flat:checked + .tgl-btn {
  border: 4px solid #7FC6A6;
}
.tgl-flat:checked + .tgl-btn:after {
  right: 0%;
  background: #7FC6A6;
}
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Pure CSS toggle buttons</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> 

</head>
<body>
    <h4>Toggle Switch</h4>
    <input class="tgl tgl-flat" id="cb4" class="radio" value="1" name="fooby[1][]" type="checkbox" checked />
    <label class="tgl-btn" for="cb4"></label>
</body>

</html>

The old code is this:

<select class="select" name="notifications" type="text">
   <option value="1" <? if ($notifications == 1) echo "selected"; ?>>ON</option>
   <option value="0" <? if ($notifications == 0) echo "selected"; ?>>OFF</option>
</select>
DigiNet Events
  • 357
  • 2
  • 14

2 Answers2

1

So what you're asking is how to handle the toggle switch? I added this javascript to your code:

$("#cb4").change(function() {
    if ($(this).is(":checked")) {
      //ON
      console.log("on");
    } else {
      //OFF
      console.log("off");
    }
});

Code snippet:

$("#cb4").change(function() {
  if ($(this).is(":checked")) {
      //ON
      console.log("on");
    } else {
      //OFF
      console.log("off");
    }
});
html,
body {
  display: flex;
  min-height: 100%;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-family: sans-serif;
}

.tgl {
  display: none;
}
.tgl, .tgl:after, .tgl:before, .tgl *, .tgl *:after, .tgl *:before, .tgl + .tgl-btn {
  box-sizing: border-box;
}
.tgl::-moz-selection, .tgl:after::-moz-selection, .tgl:before::-moz-selection, .tgl *::-moz-selection, .tgl *:after::-moz-selection, .tgl *:before::-moz-selection, .tgl + .tgl-btn::-moz-selection {
  background: none;
}
.tgl::selection, .tgl:after::selection, .tgl:before::selection, .tgl *::selection, .tgl *:after::selection, .tgl *:before::selection, .tgl + .tgl-btn::selection {
  background: none;
}
.tgl + .tgl-btn {
  outline: 0;
  display: block;
  width: 4em;
  height: 2em;
  position: relative;
  cursor: pointer;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
}
.tgl + .tgl-btn:after, .tgl + .tgl-btn:before {
  position: relative;
  display: block;
  content: "";
  width: 50%;
  height: 100%;
}
.tgl + .tgl-btn:after {
  right: -50%;
}
.tgl + .tgl-btn:before {
  display: none;
}
.tgl:checked + .tgl-btn:after {
  right: 50%;
}

.tgl-flat + .tgl-btn {
  padding: 2px;
  transition: all .2s ease;
  background: #fff;
  border: 4px solid #f2f2f2;
  border-radius: 2em;
}
.tgl-flat + .tgl-btn:after {
  transition: all .2s ease;
  background: #f2f2f2;
  content: "";
  border-radius: 1em;
}
.tgl-flat:checked + .tgl-btn {
  border: 4px solid #7FC6A6;
}
.tgl-flat:checked + .tgl-btn:after {
  right: 0%;
  background: #7FC6A6;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Pure CSS toggle buttons</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> 

</head>
<body>
    <h4>Toggle Switch</h4>
    <input class="tgl tgl-flat" id="cb4" class="radio" value="1" name="fooby[1][]" type="checkbox" checked />
    <label class="tgl-btn" for="cb4"></label>
</body>

</html>
Sacha
  • 322
  • 1
  • 8
  • As you can see in the old code every option has it's own value so i just need to define the same values for this new code – DigiNet Events Jun 06 '18 at 16:40
  • @DigiNetEvents sorry I'm not sure what you're asking.. Could you clarify exactly what you want the code to do? – Sacha Jun 06 '18 at 16:49
  • Yes sorry, i need to send the value of "ON" \ "OFF" as "1" or "0" to my database using form based on the selected option – DigiNet Events Jun 06 '18 at 17:03
  • @DigiNetEvents I'm not sure how you communicate with you database, all I see in your old code is `echo "selected"` – Sacha Jun 06 '18 at 17:12
0

I think the answer in the question below is what you are looking for.

How to make a radio button look like a toggle button

Yas Ikeda
  • 973
  • 1
  • 9
  • 16