Home > OS >  How to add link in toggle button
How to add link in toggle button

Time:09-21

i need someone to help, I have a toggle button created use HTML and CSS, I want each side to have its own link like the BN link will be directed to link A and En will be directed to link B. code like below:

HTML :

<div >
    <input
      id="language-toggle"
      
      type="checkbox"
    />
    <label for="language-toggle"></label>
    <span >BN</span>
    <span >EN</span>
  </div>

CSS:

.switch {
    position: relative;
    display: inline-block;
    margin: 0 5px;
  }
  
  .switch > span {
    position: absolute;
    top: 14px;
    pointer-events: none;
    font-family: 'Helvetica', Arial, sans-serif;
    font-weight: bold;
    font-size: 12px;
    text-transform: uppercase;
    text-shadow: 0 1px 0 rgba(0, 0, 0, .06);
    width: 50%;
    text-align: center;
  }
  
  input.check-toggle-round-flat:checked ~ .off {
    color: #F36F25;
  }
  
  input.check-toggle-round-flat:checked ~ .on {
    color: #fff;
  }
  
  .switch > span.on {
    left: 0;
    padding-left: 2px;
    color: #F36F25;
  }
  
  .switch > span.off {
    right: 0;
    padding-right: 4px;
    color: #fff;
  }
  
  .check-toggle {
    position: absolute;
    margin-left: -9999px;
    visibility: hidden;
  }
  .check-toggle   label {
    display: block;
    position: relative;
    cursor: pointer;
    outline: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }
  
  input.check-toggle-round-flat   label {
    padding: 2px;
    width: 97px;
    height: 35px;
    background-color: #F36F25;
    -webkit-border-radius: 60px;
    -moz-border-radius: 60px;
    -ms-border-radius: 60px;
    -o-border-radius: 60px;
    border-radius: 60px;
  }
  input.check-toggle-round-flat   label:before, input.check-toggle-round-flat   label:after {
    display: block;
    position: absolute;
    content: "";
  }
  
  input.check-toggle-round-flat   label:before {
    top: 2px;
    left: 2px;
    bottom: 2px;
    right: 2px;
    background-color: #F36F25;
    
    -moz-border-radius: 60px;
    -ms-border-radius: 60px;
    -o-border-radius: 60px;
    border-radius: 60px;
  }
  input.check-toggle-round-flat   label:after {
    top: 4px;
    left: 4px;
    bottom: 4px;
    width: 48px;
    background-color: #fff;
    -webkit-border-radius: 52px;
    -moz-border-radius: 52px;
    -ms-border-radius: 52px;
    -o-border-radius: 52px;
    border-radius: 52px;
    -webkit-transition: margin 0.2s;
    -moz-transition: margin 0.2s;
    -o-transition: margin 0.2s;
    transition: margin 0.2s;
  }
  
  input.check-toggle-round-flat:checked   label {
  }
  
  input.check-toggle-round-flat:checked   label:after {
    margin-left: 44px;
  }

and I just want to add a link to a BN side and EN side, like if I click in BN the link goes to google.com and if I click EN the link goes to facebook.com

CodePudding user response:

You Can simply put your BN and EN in Tag. like this:

    <div >
    <input
      id="language-toggle"
      
      type="checkbox"
    />
    <label for="language-toggle"></label>
    <a href="www.google.com"><span >BN</span></a>
    <a href="www.facebook.com"><span >EN</span></a>
  </div>

CodePudding user response:

JavaScript makes it easy, so just add this JavaScript tag to end of <body> :

  <body>
    <div >
      <input
        id="language-toggle"
        
        type="checkbox" />
      <label for="language-toggle"></label>
      <span >BN</span>
      <span >EN</span>
    </div>

    <script>

      document.getElementById("language-toggle").addEventListener("click", myFunction);

      function myFunction() {
        if (document.getElementById("language-toggle").checked) {
          window.open("https://www.google.com/", "_blank");
        } else {
          window.open("https://www.facebook.com/", "_blank");
        }
      }

    </script>

  </body>

If you want to open links in same tab change "_blank" to "_self"

CodePudding user response:

<!doctype html>
<html lang="fa" dir="rtl">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <meta http-equiv="Cache-Control" content="no-cache" />
  <meta http-equiv="Pragma" content="no-cache" />

  <style>
    .switch {
      position: relative;
      display: inline-block;
      margin: 0 5px;
    }

    .switch>span {
      position: absolute;
      top: 14px;
      pointer-events: none;
      font-family: 'Helvetica', Arial, sans-serif;
      font-weight: bold;
      font-size: 12px;
      text-transform: uppercase;
      text-shadow: 0 1px 0 rgba(0, 0, 0, .06);
      width: 50%;
      text-align: center;
    }

    input.check-toggle-round-flat:checked~.off {
      color: #F36F25;
    }

    input.check-toggle-round-flat:checked~.on {
      color: #fff;
    }

    .switch>span.on {
      left: 0;
      padding-left: 2px;
      color: #F36F25;
    }

    .switch>span.off {
      right: 0;
      padding-right: 4px;
      color: #fff;
    }

    .check-toggle {
      position: absolute;
      margin-left: -9999px;
      visibility: hidden;
    }

    .check-toggle label {
      display: block;
      position: relative;
      cursor: pointer;
      outline: none;
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }

    input.check-toggle-round-flat label {
      padding: 2px;
      width: 97px;
      height: 35px;
      background-color: #F36F25;
      -webkit-border-radius: 60px;
      -moz-border-radius: 60px;
      -ms-border-radius: 60px;
      -o-border-radius: 60px;
      border-radius: 60px;
    }

    input.check-toggle-round-flat label:before,
    input.check-toggle-round-flat label:after {
      display: block;
      position: absolute;
      content: "";
    }

    input.check-toggle-round-flat label:before {
      top: 2px;
      left: 2px;
      bottom: 2px;
      right: 2px;
      background-color: #F36F25;

      -moz-border-radius: 60px;
      -ms-border-radius: 60px;
      -o-border-radius: 60px;
      border-radius: 60px;
    }

    input.check-toggle-round-flat label:after {
      top: 4px;
      left: 4px;
      bottom: 4px;
      width: 48px;
      background-color: #fff;
      -webkit-border-radius: 52px;
      -moz-border-radius: 52px;
      -ms-border-radius: 52px;
      -o-border-radius: 52px;
      border-radius: 52px;
      -webkit-transition: margin 0.2s;
      -moz-transition: margin 0.2s;
      -o-transition: margin 0.2s;
      transition: margin 0.2s;
    }

    input.check-toggle-round-flat:checked label {}

    input.check-toggle-round-flat:checked label:after {
      margin-left: 44px;
    }
  </style>


</head>

<body>

  <div >
    <input onchange="onChange()" id="language-toggle"  type="checkbox" />
    <label for="language-toggle"></label>
    <span >BN</span>
    <span >EN</span>
  </div>
  <script>
    function onChange() {
      var state = document.getElementById("language-toggle").checked;
      if (state) {
        window.location = 'https://google.com';
        //or window.location='BNPage.html';
      } else {
        window.location = 'https://facebook.com';
        //or window.location='ENPage.html';
      }

    }
  </script>

</body>

</html>

  • Related