[whatwg] Intergrating the DOM and JavaScript (JSDOM)

Dean Edwards dean at edwards.name
Thu May 11 03:39:49 PDT 2006


Some code to illustrate:

// old way
var isChecked = false;
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
 if (inputs[i].type == "checkbox" && inputs[i].checked) {
   isChecked = true;
   break;
 }
}

// better (using iterators on NodeList)
var isChecked = document.getElementsByTagName("input").some(function(input) {
 return input.type == "checkbox" && input.checked;
});

// even better (using W3C Selectors API)
var isChecked =
document.matchAll("input[type=checkbox]").some(function(checkbox) {
 return checkbox.checked;
});


-dean



More information about the whatwg mailing list