Check multiple checkboxes with one global funtion

Kirsten de Wit

I am trying to select a list of checkboxes. The inputs are generated dynamically and there is more than one list of checkboxes - so I created a global function by sending in the id of the < table > so that the correct list of checkboxes are checked/unchecked. I think the looping of the nodes in the nodelist is causing the problem but in my mind, it makes sense, but the checkboxes are not checking and there is no error popping up either.

function checkAll(name) {
  var nodeList = $(name).next('input[type="checkbox"]');
  var nodes = $(nodeList);
  nodes.each(function(node) {
    if (!node.disabled) {
      node.checked = true;
    }
  }); 
}
<table summary="User locations" id="LocationsTable">
  <tr>
    <th>
      <input type='hidden' name='StoreSelector' id='StoreSelector' value='true'>
      Other locations (multi-store)
      <button type="button" onclick="javascript:checkAll('LocationsTable');">All</button>
      <button type="button" onclick="javascript:uncheckAll('LocationsTable');">None</button>
    </th>
  </tr>
  <tr id="LocationRow">
    <td>
      <input type="checkbox" name="Store" id="Store" value="Store">&nbsp;<span id="StoreName">Store 1</span>
    </td>
  </tr>
</table>

Aalexander

You can use find here very well. Consider that your name is an ID, so put in fron of your name the hashtag #name. Also I would recommend using an event listener. I updated your code to use it therefore I gave your btn an id.

document.getElementById('btn').addEventListener('click', function(){checkAll('LocationsTable')})


function checkAll(name) {
  var nodeList = [...$(`#${name}`).find('input[type="checkbox"]')];
  nodeList.forEach(function(node) {

    if (!node.disabled) {
      node.checked = true;
    }
  }); 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table summary="User locations" id="LocationsTable">
  <tr>
    <th>
      <input type='hidden' name='StoreSelector' id='StoreSelector' value='true'>
      Other locations (multi-store)
      <button id='btn' type="button" >All</button>
      <button type="button" onclick="javascript:uncheckAll('LocationsTable');">None</button>
    </th>
  </tr>
  <tr id="LocationRow">
    <td>
      <input type="checkbox" name="Store" id="Store" value="Store">&nbsp;<span id="StoreName">Store 1</span>
            <input type="checkbox" name="Store" id="Store" value="Store">&nbsp;<span id="StoreName">Store 1</span>

      <input type="checkbox" name="Store" id="Store" value="Store">&nbsp;<span id="StoreName">Store 1</span>

    </td>
  </tr>
</table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Multiple checkboxes in list item android

来自分类Dev

Multiple Selects into one select

来自分类Dev

Enable global middleware only for one environment in Laravel 5

来自分类Dev

Clojure, concat multiple sequences into one

来自分类Dev

Join multiple records to one row?

来自分类Dev

Multiple check box not working Rails 4

来自分类Dev

regular expressions to check length with multiple options

来自分类Dev

How to write multiple statements in one if block in clojure?

来自分类Dev

Left join multiple tables onto one table

来自分类Dev

How to undo strsplit to put multiple characters into one

来自分类Dev

Multiple synchronous streams in one Gulp task?

来自分类Dev

Multiple Summer note divs on one page

来自分类Dev

Find matching type for one of multiple joined tables

来自分类Dev

sed to replace multiple words but skip one

来自分类Dev

Opposite of tidyr::separate, concatenating multiple columns into one

来自分类Dev

Jquery - Multiple function calls to one AJAX call

来自分类Dev

制作指向两个函数的std :: funtion c ++

来自分类Dev

haskell - How to check one default radio in a radioFieldList in yesod

来自分类Dev

Array of Checkboxes in Yesod

来自分类Dev

Import multiple excel files into python pandas and concatenate them into one dataframe

来自分类Dev

Multiple UPDATE in single transaction vs one UPDATE with big WHERE clause

来自分类Dev

Neo4j gem - Querying multiple parameters in one property

来自分类Dev

Passing external function of multiple variables as a function of one variable in Fortran

来自分类Dev

Add multiple values to localStorage and then retrieve a random one in JavaScript

来自分类Dev

Use Octave/Matlab combine multiple blobs in close proximity into one blob

来自分类Dev

How to add multiple class file in one XAML file in WPF

来自分类Dev

Without regex reduce multiple spaces in a string to one in Python

来自分类Dev

Copy and Paste a range from one worksheet to multiple worksheets

来自分类Dev

Nhibernate Multiple table have One to many relation to specific table

Related 相关文章

  1. 1

    Multiple checkboxes in list item android

  2. 2

    Multiple Selects into one select

  3. 3

    Enable global middleware only for one environment in Laravel 5

  4. 4

    Clojure, concat multiple sequences into one

  5. 5

    Join multiple records to one row?

  6. 6

    Multiple check box not working Rails 4

  7. 7

    regular expressions to check length with multiple options

  8. 8

    How to write multiple statements in one if block in clojure?

  9. 9

    Left join multiple tables onto one table

  10. 10

    How to undo strsplit to put multiple characters into one

  11. 11

    Multiple synchronous streams in one Gulp task?

  12. 12

    Multiple Summer note divs on one page

  13. 13

    Find matching type for one of multiple joined tables

  14. 14

    sed to replace multiple words but skip one

  15. 15

    Opposite of tidyr::separate, concatenating multiple columns into one

  16. 16

    Jquery - Multiple function calls to one AJAX call

  17. 17

    制作指向两个函数的std :: funtion c ++

  18. 18

    haskell - How to check one default radio in a radioFieldList in yesod

  19. 19

    Array of Checkboxes in Yesod

  20. 20

    Import multiple excel files into python pandas and concatenate them into one dataframe

  21. 21

    Multiple UPDATE in single transaction vs one UPDATE with big WHERE clause

  22. 22

    Neo4j gem - Querying multiple parameters in one property

  23. 23

    Passing external function of multiple variables as a function of one variable in Fortran

  24. 24

    Add multiple values to localStorage and then retrieve a random one in JavaScript

  25. 25

    Use Octave/Matlab combine multiple blobs in close proximity into one blob

  26. 26

    How to add multiple class file in one XAML file in WPF

  27. 27

    Without regex reduce multiple spaces in a string to one in Python

  28. 28

    Copy and Paste a range from one worksheet to multiple worksheets

  29. 29

    Nhibernate Multiple table have One to many relation to specific table

热门标签

归档