Manage identical checkbox across various locations

As someone who is just starting out with HTML, CSS, and JavaScript coding, I am faced with the task of adding multiple checkboxes to a single page that also contains multiple tabs.

I am seeking guidance on how to achieve a functionality where selecting or deselecting any checkbox will automatically update the checkboxes in all locations on the page. Can anyone provide instructions on how to accomplish this using HTML, CSS, and JavaScript?

Answer №1

Make sure to assign a common name to those checkboxes, and then implement the following JavaScript code:


function checkAll() {
var field = document.your_form_name.your_checkbox_name;
for (i = 0; i < field.length; i++)
    field[i].checked = true ;
}

Hopefully this solution works for you!

Answer №2

<html>
<head>
<title>JavaScript - Implementing Check All Functionality in a Form</title>
<script>
var fieldName='chkName';

function selectall(){
  var totalElements=document.frm.elements.length;
  var allElements=document.frm.elements;
  var nameArray=new Array();
  var valueArray=new Array();
  var j=0;
  for(var k=0;k<totalElements;k++)
  {
    if(document.frm.elements[k].name==fieldName)
    {
      if(document.frm.elements[k].checked==true){
        valueArray[j]=document.frm.elements[k].value;
        j++;
      }
    }
  }
  checkSelect();
}
function selectCheck(obj)
{
 var totalElements=document.frm.elements.length;
  for(var k=0;k<totalElements;k++)
  {
    if(document.frm.elements[k].name==fieldName)
    {
      document.frm.elements[k].checked=obj;
    }
  }
  selectall();
}

function selectallMe()
{
  if(document.frm.allCheck.checked==true)
  {
   selectCheck(true);
  }
  else
  {
    selectCheck(false);
  }
}
function checkSelect()
{
 var totalElements=document.frm.elements.length;
 var hasError=true;
  for(var k=0;k<totalElements;k++)
  {
    if(document.frm.elements[k].name==fieldName)
    {
      if(document.frm.elements[k].checked==false)
      {
        hasError=false;
        break;
      }
    }
  }
  if(hasError==false)
  {
    document.frm.allCheck.checked=false;
  }
  else
  {
    document.frm.allCheck.checked=true;
  }
}
</script>
</head>

<body>
<form name="frm">
select all :<input type="checkbox" name="allCheck" onClick="selectallMe()">
<hr><br>
1  :<input type="checkbox" name="chkName" onClick="selectall()"><br>
2  :<input type="checkbox" name="chkName" onClick="selectall()"><br>
3  :<input type="checkbox" name="chkName" onClick="selectall()"><br>
4  :<input type="checkbox" name="chkName" onClick="selectall()"><br>
5  :<input type="checkbox" name="chkName" onClick="selectall()"><br>
6  :<input type="checkbox" name="chkName" onClick="selectall()"><br>
7  :<input type="checkbox" name="chkName" onClick="selectall()"><br>
8  :<input type="checkbox" name="chkName" onClick="selectall()"><br>
9  :<input type="checkbox" name="chkName" onClick="selectall()"><br>
10 :<input type="checkbox" name="chkName" onClick="selectall()"><br>
</form>
</body>
</html>

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

Exploring Complex Parent-Child Connections with FeathersJS

Is there an optimal method for managing deep nested parent-child relationships in Feathers using the recommended Association Method? Let's say our data entries look like this: [ {"name": "Grandpa", "id": 1, "parent": null}, {"name": "Mom", " ...

Is it possible to eliminate all inline styles on a parent element using a child element in CSS?

I'm currently working on an Angular project and I need to remove the styling of the parent element by using the child element, based on certain conditions. It's important to note that the parent element has inline styling. My code looks somethin ...

Is it possible to preload numerous extensive datasets in the background using ajax?

I'm currently in the process of developing a web application that operates solely on one page and revolves around presenting various table data in grids. There are approximately 30 different tables stored in the database, with any of them being access ...

Mapping objects in an array with Javascript

This code snippet is intended for a React Native Chat app. The structure of my data should look something like this: const chatData = [ { id: 1, name: 'John Doe', messages: [ {text: 'Hello', sentAt: 'time here' ...

Using Ajax to implement Basic Authentication for securely accessing an https-protected webpage without requiring users to manually enter their username and password in a

Is there a way to access and display a website page hosted at this URL - , without encountering any authentication dialog box from my application on the same network? I have been unable to bypass the username and password entry request. I successfully imp ...

Issues with quotation marks in the output of an ASP.NET Core Razor view

I need help displaying my texteditor content in a razor view. The content contains HTML tags and is saved in a database, but when I run my website it appears as a string value. Here is the value stored in the database: https://i.sstatic.net/gOV9h.jpg Now, ...

Error message "google is not defined" appears when trying to access @react-google-maps/api within the useEffect hook

My Current Challenge I am currently attempting to place a marker on the map and calculate the distance between the marker and a fixed location on the map. If the distance exceeds 1000 meters, I want to display a modal indicating that the ...

Are there any alternative methods to override CSS besides using !important or adjusting the specificity of the element?

I'm currently working on styling a webpage and facing a challenge. I am limited to editing only the CSS file without being able to touch the HTML code. Despite using !important and specificity, I am struggling to override some existing styles. How can ...

Highlighting syntax on the server side

I have some code blocks that I want to emphasize. Currently, I am using prismjs for this purpose. However, when I try to highlight several code blocks, the page takes more than 5 seconds to load on my somewhat slow PC. Interestingly, if I remove the prism ...

What is the essential Angular 2 script that must be included for a simple Angular 2 application to function properly?

I'm currently working through the latest Tuts+ tutorial on Angular 2 In the tutorial, the author references adding this script: <script src="node_modules/angular2/bundles/angular2.sfx.dev.js"></script> However, in the most recent beta re ...

I'm wondering why my positive numbers aren't displayed in green and negative numbers in red

I am currently working on a commodities quotes widget. I have already set up the 'Current' and '24-hour' divs, but I'm facing an issue where positive values are not displaying in green and negatives in red as intended. I have check ...

Deactivate the selection option in Syncfusion NumericTextbox

I have integrated an Angular NumericTextbox component from Syncfusion into my application. A problem we encountered is that when the input is clicked, it automatically gets selected. Is there a way to disable this behavior? Problem: https://gyazo.com/a72b ...

How can I display the value entered in a text input field when using ajax upload?

Is there a way to display the value of an input type text when using ajax for file upload? I have successfully implemented code to upload files to a directory called attachments_files, but I am facing an issue. How can I retrieve and echo the value from i ...

Can you explain the concept of "Import trace for requested module" and provide instructions on how to resolve any issues that

Hello everyone, I am new to this site so please forgive me if my question is not perfect. My app was working fine without any issues until today when I tried to run npm run dev. I didn't make any changes, just ran the command and received a strange er ...

Experiencing a horizontal scroll while shifting the elements despite no visible overflow

While working on the contact section of my website, I encountered an issue. Every time I adjust the position of the textboxes and labels by 15em within the wrapper, a horizontal scroll appears. How can I move these elements without triggering the scroll ba ...

What is the best way to pass a custom HTTP header to a Node.js server?

One of my current challenges involves utilizing jQuery to include a custom header in each AJAX request. $.ajaxSetup({ beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", "Basic " + btoa(user + ":" + sessionID)); }, }); My ...

Autoheight iframes, similar to those found on eBay,

Is there a method to adjust the height of an iframe in the y-axis so that the content properly fits within it? The challenge lies in finding solutions that work within the constraints imposed by modern CORS and Same Origin policies enforced by web browser ...

The website no longer uses AJAX calls and does not display any errors, although it used to do so in the past

I've noticed that my website is no longer making the ajax call it used to make before I made some changes to my code. However, I can't seem to figure out what exactly I changed. The function call appears to be correct as it triggers the initial a ...

What is the method for transforming the text entered by a user into uppercase using a function?

I'm currently in the process of working on a project for my introductory JavaScript and HTML course. I'm trying to create a feature that will convert text entered into a text box to all uppercase letters when a button is clicked. Here's what ...

Using `babel/register` does not seem to be functioning as expected

I am working on an isomorphic app built in ES6 on the client side using the Babel transpiler. I would like my express server to also utilize the same ES6 syntax as the client code. Unfortunately, attempting to use require('babel/register') has n ...