Document.getElementById ("selAge") Vs document.myForms.selAge

What's the difference between

document.getElementById("selAge") 
document.myForms.selAge

      

When to use which one?

+1


source to share


4 answers


document.getElementById () is the recommended way to get a reference to elements. It is easier to use and also doesn't require any changes if you decide to change the form name or ID. However, since it traverses the entire DOM tree, it tends to be slower than document.forms notation of object references, so keep that in mind for a lot of such requests.



document.forms is also more difficult to use, since you need to know the entire path to the object starting at the document element.

0


source


These are two different paths to the same goal that you use depends on your preference. Personally, I would use document.getElementById("selAge")

, because if the structure of your HTML changes, it will still work.



0


source


document.myForms.selAge is part of the DOM specification and should therefore not be unnecessarily avoided in favor of getElementById. Use whatever is easier.

0


source


The second is an absolute no-no! It first finds an element with id / name "myForms" and gets its child with id / name "selAge". The first one finds any element with the identifier "selAge". Always use the first ... because someone in the future might decide to put "myForms" in a div. Then you have a fix.

0


source







All Articles