Tips for modifying an input verification attribute

When I select the username1 option from the dropdown, the toggle for spec view in the right card should turn off. However, the code is not functioning as expected. 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>
            // More labels and checkbox inputs here...

          </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);
     
  });
});

The Bootstrap toggle library used: Project link:https://codepen.io/nutkin/pen/KKgJNMd

Answer №1

Based on the information provided in the official documentation:

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

As a result, it is necessary to update your click event handler as follows:

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

This code snippet showcases how this change should be implemented:

$("#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>

Answer №2

If you're combining JavaScript with jQuery, it's recommended to use the toggle class to turn on and off the switch. Check out this example project here

$(document).ready(function(){
  $("#username1").click(function(){
    $('#specview').parent('.toggle').toggleClass('on off');
    $('#specview').removeAttr('checked');
  });
});
Here is the modified code snippet:

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

This includes relevant links to external resources for Bootstrap and other libraries.

Answer №3

To implement the functionality, you can refer to the code snippet below:

Verify

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

Reverse

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to collapse a button in asp.net using javascript after setting it to hidden?

I have a scenario where I have 3 buttons in a row on my asp.net page. Using JavaScript, I am setting the middle button (ButtonSR) to invisible. However, I want the button below it to move up and take its place instead of leaving an empty space. ...

Utilizing Bootstrap's alert components

<% if(success && success.length) {%> <div class="alert alert-success alert-dismissible fade show" role="alert"> <%= success %> <button type="button" class="close" data ...

Popup showing values that are not defined

I've encountered an issue with tooltips on my bar graph. The tooltips should display the values of boarding and alightings corresponding to each stopname column, but I'm seeing undefined values like this Below is my code snippet: <!DOCTY ...

Issue with Angular4 select in dropdown not able to trigger onChange event

I am new to working with Angular 4 and I am currently in the process of building something. In my project, I have multiple components, one of which contains the following HTML code: <div class="row" style="border:1px solid black;"> <br> <d ...

Tips for managing the sub query in nodejs?

Developed a RESTful API in nodeJS focusing on a Post-type feature. The process involves executing two queries: 1. Initially fetching user-Id and answer details from the answers table. Upon checking the console, two user-Ids are displayed. 2. Secondly, que ...

store the id of each li element dynamically into an array

In my code, a list is generated dynamically and each list item has a special id. I am trying to store each li "id" in one array. This is JavaScript code: var i = 0; $("ul#portfolio li").each(function(eval) { var idd = new Array(); idd[i] = $(this ...

What is the best way to eliminate the horizontal line in the modal's body and footer?

How can I eliminate the horizontal lines from my modal? $(document).ready(function(){ $('#myModal').modal('show'); }); <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity= ...

When attempting to add a new row to a table, the function fails to function properly

I am facing an issue with my dynamic HTML table. Each row in the table should have two cells whose values are multiplied together. I have created a function to achieve this, but it only works on the first row of the table and not on any new rows added by c ...

What is the best way to clear a MongoDB objectId field, set it to null, or ultimately remove it from a

Custom Modal Schema : { "title":{type:String,required:true}, "genre":{type:mongoose.Schema.Types.ObjectId,ref:"Genre"} } Upon creating a document using this schema, the document structure appears as follows: { "_id":ObjectId("5abcde12345fgh6789ijk ...

Issue encountered when trying to attach a hover event to the items in a comb

Currently, I am facing a specific situation. The requirement is to display a custom tooltip when the mouse hovers over the combobox items (specifically the "option" tag). Initially, my solution involved using the title tag. While this method worked effecti ...

Utilizing 'this' in jQuery: Image swapping with thumbnails, Twitter Bootstrap framework

Is it possible for jQuery's 'this' to simplify my code? You can view the full code here. Thank you for any help or suggestions. Here is the jQuery code: /* Ref: http://api.jquery.com/hover/ Calling $( selector ).hover( handlerIn, handler ...

Display a confirmation modal before triggering $routeChangeStart in AngularJs, similar to the window.onbeforeunload event

When a user chooses to stay on the page as the route starts to change, the original route remains intact but the form directives are reloaded. This results in the loss of all checkbox and input values, resetting them to their defaults. If a user closes th ...

Having issues with my JavaScript code - it just won't function

I am having an issue with my JavaScript code. I don't receive any errors, but when I click the submit button, nothing happens. I have followed a video tutorial and watched it twice, but I can't seem to figure out what is wrong here. Here is the ...

Having difficulty changing the visibility of a div element

I am currently working on a project that involves jQuery and ASP.Net. My main goal is to create a button that can hide/show a div using jQuery. Below is the code that I have implemented: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLI ...

Implementing a Javascript solution to eliminate the # from a URL for seamless operation without #

I am currently using the pagepiling jQuery plugin for sliding pages with anchors and it is functioning perfectly. However, I would like to have it run without displaying the '#' in the URL when clicking on a link like this: www.mysite.com/#aboutm ...

Acquiring the parent object (a group) from the children in Three.js

In the scenario I have created, there are multiple groups of objects (Object3Ds) and I have established a mechanism for clicking or hovering over them to trigger specific actions. However, when using the raycaster to identify the objects under the cursor, ...

Failure to successfully transmit data from Ajax to PHP script

When I click on a button in my HTML page, I am trying to retrieve information from my PHP code, but it is not connecting properly. The front page displayed in index.php is as follows: <!DOCTYPE html> <html> <head> <link rel="styleshe ...

Tips for ensuring that the callback method waits for the completion of Google Markers creation

While developing my app with the Google Maps library, I encountered an issue either due to an unexplainable delay in creating markers or an unseen asynchronous problem. Here is a breakdown of the situation: The code retrieves the locations of Electric Cha ...

What is the reason behind JavaScript's `fn.length` returning the count of named parameters that `fn` function has?

Why does calling fn.length in JavaScript return the number of named arguments fn has? > function fn () { } > x.length 0 > function fn (a) { } > x.length 1 > function fn (a,b,c) { } > x.length 3 This behavior is quite peculiar. I wonde ...

Visualizing JSON data in React.js using Chart.js

Currently, as a beginner in ReactJS, I am working on an application that displays COVID-19 data from a JSON API in a visual format using Chart.js. However, despite trying various solutions, I am unable to visualize the data. Below is my source code: App ...