getElementById('x').value vs. x.value
Both versions of this javascript work equally well from what I can tell, though intuition tells me that using the full document.getElementById().value is more "proper." I'd like to use the shorter version as it just looks cleaner on the page, but before I go nuts shortening everything, I'd like to know a couple things:
1.) What is the theoretical/computational/something-to-that-effect difference?
2.) What are the disadvantages/advantages of each version?
Code:y = parseFloat(document.getElementById('x').value); document.getElementById('x').value = y.toFixed(2);Assume that "x" is the id of a textbox and that its value is a number. Also, the code above is generalized. My actual code is the same thing with different variables and repeated several times.Code:y = parseFloat(x.value); x.value = y.toFixed(2);
(Another note: I want "y" stored for later computation, so it's not completely frivolous.)
Browsers tested: IE 6 and Firefox 2.0 and Opera 9.26.



Reply With Quote



Bookmarks