[whatwg] Intergrating the DOM and JavaScript (JSDOM)

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

Received on Thursday, 11 May 2006 03:39:49 UTC