1

Unfortunately there still are forms were bulk/mass editing is not implemented. What can be very annoying, especially when you've a form with over 100 records what you've to update. In my case it's around 1000 records and each record has a checkbox what should be checked, currently they all are unchecked.

Therefor I tried to run an on-the-fly piece of javascript to check those boxes for me. However it did show a checkmarker at all checkboxes after running the javascript. After submission the form the first unchecked checkboxes - what were shown as checked checkbox - returned back to there original state (unchecked).

The on-the-fly javascript I executed from FireFox's console.

var x = document.querySelectorAll('input[name^="example"][type="checkbox"]'), i = 0;
for (i = 0; i < x.length; i++) {
    x[i].checked = true;
    x[i].value = true; // I also tried 'on'
}

The initial markup of checkboxes (manually) checked and unchecked:

<input name="example[1]" checked="" type="checkbox">
<input name="example[2]" type="checkbox">

As far as I know the checked attribute is only there for visual representation, to allow programmers to show a checked box on initialization of the element. Also good to note is when you uncheck a checkbox than the element won't change.

Am I using the right approach? If so than what I'm doing wrong? Or do I've to use a different approach to reach my goal, what should I do?

luukvhoudt
  • 1,726
  • 19
  • 33
  • "But after submitting the form the checkboxes returned back to there original state (unchecked)." If you don't do anything in the server-side code to affect the status of the checkboxes as they are rendered to the page, then they won't automatically change value, no. HTTP is stateless, it doesn't remember what things were like before the current request, unless you take steps to implement some form of persistence layer. – ADyson Jul 28 '16 at 13:40
  • @ADyson Unfortunately I'm not aware of the source, but I guess that it looks at the `$_POST['example']` variable what by default is set to `true` or `'on'` at the value of a checkbox input after checking the box (not visible in the markup), right? Basically I'm trying to modify this post variable with the webform, perhaps I should use postman? – luukvhoudt Jul 28 '16 at 13:56
  • an oddity of HTML forms is that if a checkbox isn't checked, there won't even be a $_POST variable present for it when the postback happens. I was talking about what happens when it renders the page a second time - if the code that renders the page doesn't take into account whether any given checkbox had a $_POST variable or not, then that would explain why it doesn't "remember" whether you checked it or not before submitting. – ADyson Jul 28 '16 at 14:57

1 Answers1

2

Try using the following:-

x[i].setAttribute('checked', 'checked');

If you inspect the input after running your original code you can see it only sets the value attribute

<input name="example[2]" type="checkbox" value="true">

This differs from the html where you manually click the checkbox, where the checked attribute has been set.

Joe
  • 1,847
  • 2
  • 17
  • 26
  • Unfortunately it didn't work, i think there is no difference between `x[i].setAttribute('checked', $foo)` and `x[i].checked=$foo`, where `$foo` is either `true`, `''` or `'checked'`. – luukvhoudt Jul 28 '16 at 14:04
  • 1
    You could try and trigger the click event on the non checked inputs with - if(!x[i].checked) { x[i].click(); } – Joe Jul 28 '16 at 14:57