When should I use getElementById?

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. GetElementById to obtain a reference to it (you already have one).

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. GetElementById to obtain a reference to it (you already have one): TEST The place where you might want to use document.

GetElementById is when you want to control another element to which you don't already have a reference: TEST DIV REVEALED Note that there is rarely (if ever) a reason to do something like the following: TEST 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.

Thank you very much, not only for the answer but for educating me for the more-to-come. – CindyH Aug 20 '09 at 14:41.

If e is the node, there's no need to write document. GetElementById(e. Id), you will get the same.

Thank you very much. – CindyH Aug 20 '09 at 14:39.

Document.getElementById() just looks for a DOM object by a given ID (string) and returns it if it's found. But e in this case is just an arbitrary variable that might be an DOM object, or it might be something like a string. It depends on the context of the rest of your source code.GetElementById() is necessary if your code looks like this: getElementById() is unnecessary if your code looks like this: The "this" part will pass a reference to the DOM object (a textbox in this case) which is really what you're after.So document.

GetElementById is unnecessary. The same example could be applied to the click of a button, onmouseover of a link, etc, etc.Generally I prefer to use the "this" method whenever possible since it'll clean up your javascript. But sometimes it's not suitable.

So then I use a naming convention in the javascript that helps me remember which method I used. Like I'll use the variable name of e for a DOM object, or id for a string. This should help you keep it straight in your code.

Thank you very much for your detailed answer. – CindyH Aug 20 '09 at 14:40.

There is no reason to do this. E. Value = cleanNumber(e.

Value).

Thank you very much. – CindyH Aug 20 '09 at 14:40.

By the looks of it your passing the element into your function but to get the element from the dom you would do: var e = document. GetElementById('elementId').

Thank you for the clarification! – CindyH Aug 20 '09 at 14:42.

Precisely speaking, the function function f (e) { return document. GetElementById(e. Id); } is the identity function everywhere it is defined, assuming that e is in document.

Thanks - I appreciate the clarification. – CindyH Aug 20 '09 at 14:41.

Window. NodeId is a non-standard IE extension -- and only present in the most recent versions of firefox, and it logs a warning whenever you make use of this. But the other issues are that if anyone ever defines a variable or function that has a name that conflicts with a node identifier then that variable or function overrides your node reference.

Together these specifics mean that you cannot guarantee entirely correct behaviour when using window. NodeId whereas document. GetElementById always works correctly.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions