-2

I have 100 Checkboxes on my web page. Checkboxes are checked through javascript mouse click event. For testing purposes I want to check all those boxes, but manually clicking is time consuming. Is there a possible way to get them checked through mouse click event without using single mouse click?

Perhaps a JavaScript or Chrome Console window, anything?

Raza
  • 1
  • 1

3 Answers3

1

You could iterate through all checkboxes and set checked to true: document.querySelectorAll('input[type="checkbox"]').forEach(el => el.checked = true) use the code from this answer. Snippet updated.

const toggleChecked = () => 
  document.querySelectorAll('input[type="checkbox"]')
    .forEach(el => simulate(el, 'click'))
    
    
//https://stackoverflow.com/a/6158050/3755425

function simulate(element, eventName)
{
    var options = extend(defaultOptions, arguments[2] || {});
    var oEvent, eventType = null;

    for (var name in eventMatchers)
    {
        if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }

    if (!eventType)
        throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

    if (document.createEvent)
    {
        oEvent = document.createEvent(eventType);
        if (eventType == 'HTMLEvents')
        {
            oEvent.initEvent(eventName, options.bubbles, options.cancelable);
        }
        else
        {
            oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
            options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
            options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
        }
        element.dispatchEvent(oEvent);
    }
    else
    {
        options.clientX = options.pointerX;
        options.clientY = options.pointerY;
        var evt = document.createEventObject();
        oEvent = extend(evt, options);
        element.fireEvent('on' + eventName, oEvent);
    }
    return element;
}

function extend(destination, source) {
    for (var property in source)
      destination[property] = source[property];
    return destination;
}

var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
}

var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">

<button onClick="toggleChecked()">Toggle checked</button>
Mohrn
  • 816
  • 4
  • 15
  • is This Code helpful for mouse click event? I want the code that is helpful for mouse click event not just simple check the unchecked boxes. – Raza Aug 31 '20 at 19:28
  • Ah, got it. I updated my answer with creating a click event rather than just checking the box. I haven't gone through the code from the other answer in detail, but seems legit. – Mohrn Aug 31 '20 at 19:36
  • There is a problem. Problem is that you use a button that is clicked for check all checkboxes. I want no button is used for check all checkboxes. I want that the all checkboxes are automatically checked with mouse click event when the code is running in console. – Raza Aug 31 '20 at 19:44
  • Then just run the code in console. The button is just there to be helpful, you don't have to actually add it :) – Mohrn Aug 31 '20 at 19:59
  • I wnat a code that is supported with internet explorer 11 – Raza Sep 01 '20 at 03:34
0

Updated answer, now will trigger click on checkbox and check it.

let chks = document.querySelectorAll("input[type='checkbox']");
       chks.forEach(function(e) {
            e.addEventListener("click", function() {
              console.log("checkbox click");
         })
      })


    if ("createEvent" in document) {
      var evt = document.createEvent("HTMLEvents");
      evt.initEvent("click", false, true);

      chks.forEach(function(el) {
        el.setAttribute("checked", true);
        el.dispatchEvent(evt);
      });
    }
<input type="checkbox"><input type="checkbox"><input type="checkbox"><input type="checkbox"><input type="checkbox"><input type="checkbox"><input type="checkbox"><input type="checkbox"><input type="checkbox"><input type="checkbox"><input type="checkbox">
<input
  type="checkbox"><input type="checkbox"><input type="checkbox"><input type="checkbox">
imvain2
  • 15,480
  • 1
  • 16
  • 21
  • is This Code helpful for mouse click event? I want the code that is helpful for mouse click event not just simple check the unchecked boxes. – Raza Aug 31 '20 at 19:28
  • @Raza I have updated my answer, it will now trigger a click event and check the checkbox – imvain2 Aug 31 '20 at 19:45
  • This code is not supported to internet explorer 11. – Raza Sep 01 '20 at 03:22
0

Will this JQuery work?

$('input:checkbox').prop('checked', true);
Reza Razani
  • 170
  • 2
  • 8
  • is This Code helpful for mouse click event? I want the code that is helpful for mouse click event not just simple check the unchecked boxes. – Raza Aug 31 '20 at 19:28
  • I want no button is used for check all checkboxes. I want that the all checkboxes are automatically checked with mouse click event when the code is running in console. – Raza Aug 31 '20 at 19:45
  • I wnat a code that is supported with internet explorer 11. – Raza Sep 01 '20 at 03:30