-1

while I click the username1 in dropdown,sepc view toggle in the right card would be off.But the code not work. html code:

<div class="container">
  <div class="row row-cols-2">
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div class="dropdown">

            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"  aria-haspopup="true" aria-expanded="false">
              Dropdown button
            </button>
            
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#" id="username1">Username1</a>
              <a class="dropdown-item" href="#" id="username2">Username2</a>
              <a class="dropdown-item" href="#" id="username3">Username3</a>
            </div>

          </div>
        </div>
      </div>

    </div>
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div id="switch">

            <label for="fname">Spec View</label>&nbsp&nbsp<input id="specview" type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>

            <label for="fname">Spec Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Attribute Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operation Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Section Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Cat-Template</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Template Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Enumeration</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">User Role</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Formula</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Data Mapping</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operating Log</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
          </div>
        </div>
      </div>
    </div>

  </div>
</div>

js code:

$(document).ready(function(){
  $("#username1").click(function(){
  var elmnt = document.getElementById("specview");
  var attr = elmnt.getAttributeNode("checked");
  elmnt.removeAttributeNode(attr);
     
  });
});

Bootstrap toggle I use:https://www.bootstraptoggle.com/ My project link:https://codepen.io/nutkin/pen/KKgJNMd

juexu
  • 131
  • 2
  • 11

3 Answers3

1

According to the documentation:

$('#toggle-demo').bootstrapToggle('on') // Sets the toggle to 'On' state

Hence, you need to change you click event handler to:

$("#username1").on('click', function(e) {
    $('#specview').bootstrapToggle('on');
});

The snippet:

$("#username1").on('click', function(e) {
    $('#specview').bootstrapToggle('on');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.bundle.min.js"></script>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

<div class="container">
  <div class="row row-cols-2">
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div class="dropdown">

            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"  aria-haspopup="true" aria-expanded="false">
              Dropdown button
            </button>
            
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#" id="username1">Username1</a>
              <a class="dropdown-item" href="#" id="username2">Username2</a>
              <a class="dropdown-item" href="#" id="username3">Username3</a>
            </div>

          </div>
        </div>
      </div>

    </div>
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div id="switch">

            <label for="fname">Spec View</label>&nbsp&nbsp<input id="specview" type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>

            <label for="fname">Spec Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Attribute Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operation Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Section Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Cat-Template</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Template Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Enumeration</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">User Role</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Formula</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Data Mapping</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operating Log</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
gaetanoM
  • 41,594
  • 6
  • 42
  • 61
0

Please do as following code example:

Check

document.getElementById("checkbox").checked = true;

Uncheck

document.getElementById("checkbox").checked = false;
B.S.
  • 668
  • 1
  • 5
  • 15
0

You mixed javascript in jquery. Actually you need to use toggle class to on off the switch Example Project Here https://codepen.io/exclutips/pen/qBavRWM

$(document).ready(function(){
  $("#username1").click(function(){
    $('#specview').parent('.toggle').toggleClass('on off');
    $('#specview').removeAttr('checked');
  });
});

$(document).ready(function(){
  $("#username1").click(function(){
    $('#specview').parent('.toggle').toggleClass('on off');
    $('#specview').removeAttr('checked');
  });
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css" rel="stylesheet"/>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js"></script>
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>

<div class="container">
  <div class="row row-cols-2">
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div class="dropdown">

            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"  aria-haspopup="true" aria-expanded="false">
              Dropdown button
            </button>
            
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#" id="username1">Username1</a>
              <a class="dropdown-item" href="#" id="username2">Username2</a>
              <a class="dropdown-item" href="#" id="username3">Username3</a>
            </div>

          </div>
        </div>
      </div>

    </div>
    <div class="col">
      <div class="card">
        <div class="card-body">
          <div id="switch">

            <label for="fname">Spec View</label>&nbsp&nbsp<input id="specview" type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>

            <label for="fname">Spec Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Attribute Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operation Edit</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Section Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Cat-Template</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Template Design</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Enumeration</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">User Role</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Formula</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Data Mapping</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
            <label for="fname">Operating Log</label>&nbsp&nbsp<input type="checkbox" checked data-toggle="toggle" data-onstyle="info" data-offstyle="light"></br>
          </div>
        </div>
      </div>
    </div>

  </div>
</div>
Firefog
  • 3,094
  • 7
  • 48
  • 86