You can use getElementsByTagName
to get all input
elements:
var inputs = document.getElementsByTagName("input"); for(var i = 0; i < inputs.length; i++) { if(inputs[i].type == "checkbox") { inputs[i].checked = true; } }
Here is an example. If you only care about new browsers, you can use querySelectorAll
:
var inputs = document.querySelectorAll("input[type='checkbox']"); for(var i = 0; i < inputs.length; i++) { inputs[i].checked = true; }
And an example of this. As an answer, you mentioned in your question that when using jQuery you can do this with each
. You do not need to use each
, since most jQuery methods work with all elements in the corresponding set, so you can only do this on one line:
$("input[type='checkbox']").prop("checked", true);
source share