I am new to document object model. Is there any java-doc like thing for DOM. I am having tough time figuring out the following javascript code.
myElements = document.getElementById('idName').elements // whose attribute is elements part of ? Element Object or Node?
for (var eachElement in myElements) {
if (myElement[eachElement].type == 'checkbox' ) {
// why do I have to do this? Specifically, why can't type be accessed as eachElement.type == 'checkbox'
}
}
I think the bigger problem is I am having tough time in accessing documentation. Any clues on both would be appreciated.
As already mentioned, the MDC documentation is quite comprehensive.
.elementsreturns aHTMLCollection[docs]. This is an array-like data structure which can be traversed via afororfor...in[docs] loop.for...inloops over properties of objects. The property name (the index, so to speak, not its value) is stored in the loop variable, hence, to access the corresponding value, you have to writeobj[prop].That is also the reason why you should not use
for...inhere. You don’t know whether it also loops over other properties of the collection that are not elements.Use a normal
forloop:I suggest to also read the JavaScript guide to learn more about loops, arrays and objects.