I want to be able to automatically mark all the checkboxes on a page EXCEPT the first one.
I have found a javascript snippet that selects all the checkboxes, that is currently working for that purpose, but I don't want it to select the first checkbox...
[].forEach.call(document.querySelectorAll('input[type="checkbox"]'), function (el) {el.checked = true});
I've tried various If statements within the above snippet, all of which break the process.
Any help will be appreciated. Note:(I am implementing this as a bookmarklet.)
The callback function can take a second parameter that is the index
. So add a condition that index !== 0
, that is function (el, index) { if (index !== 0) { el.checked = true; } }
function (el, index) { if (index !== 0) { el.checked = true; } }
.
Or even function (el, index) { el.checked = index !== 0; }
function (el, index) { el.checked = index !== 0; }
You could change your query selector to add :not(:first-child)
[].forEach.call(document.querySelectorAll('input[type="checkbox"]:not(:first-child)'), function (el) {el.checked = true});
Fiddle: https://jsfiddle.net/zLhhyy7m/2/
Or with jQuery:
$('input[type="checkbox"]:not(:first)').attr("checked",true);
Fiddle: https://jsfiddle.net/zLhhyy7m/1/
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
for (var i = 0; i < checkboxes.length; ++i) {
if (i)
checkboxes[i].checked = true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.