I’m an experienced developer, but new to javascript. I can’t figure out when I should use the whole “document.getElementById(e.id).value” and when I can just use “e”. I’m looking at a tiny existing function in a common functions script that uses both.
function RemoveFormat(e) {
document.getElementById(e.id).value =
cleanNumber(document.getElementById(e.id).value);
e.select(); }
(where cleanNumber is another common function)
Presumably there are circumstances that make the extra typing necessary, but what are they?
Thanks!
Your example demonstrates when you have a reference to the element you want to control. If you already have a reference to the element, there is no reason to use
document.getElementByIdto obtain a reference to it (you already have one):The place where you might want to use
document.getElementByIdis when you want to control another element to which you don’t already have a reference:Note that there is rarely (if ever) a reason to do something like the following:
If the function is being fired from an element event, you can just pass
this(as per my first example), rather than passing the id of the element the event is happening on.