When i use my form elements I start by doing this:
var form_elements = document.bookmark.elements; // line 1
From there I use them as such
form_elements.url
form_elements.title
I know it’s not good to access the DOM too much if you have to but I also know it’s not good to have excess variables. So that I could eliminate line 1 and just use
document.bookmark.elements.url
document.bookmark.elements.title
but then I wonder if I’m hitting the DOM on each line.
Which was is best practice?
Thanks!
What you are doing is fine. Property access on an object is likely no slower than variable look–up, which is just property access on the local variable object, so almost exactly the same thing.
Use variables to shorten the lookup chain (performance, maybe) or save typing (less likelihood of typos). Just use what suits.