W3C home > Mailing lists > Public > whatwg@whatwg.org > May 2006

[whatwg] Intergrating the DOM and JavaScript (JSDOM)

From: Dean Edwards <dean@edwards.name>
Date: Thu, 11 May 2006 11:39:49 +0100
Message-ID: <180c22a90605110339h31bc752as596b39aa784a05aa@mail.gmail.com>
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

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:08:27 UTC