tags:

views:

289

answers:

7

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!

+5  A: 

if e is the node, there's no need to write document.getElementById(e.id), you will get the same.

Philipe Fatio
Thank you very much.
CindyH
+1  A: 

There is no reason to do this.

e.value = cleanNumber(e.value);
ChaosPandion
Thank you very much.
CindyH
A: 

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')
Rigobert Song
Thank you for the clarification!
CindyH
+2  A: 

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:

<input id="mytext" type="text" onchange="RemoveFormat('mytext')" />

getElementById() is unnecessary if your code looks like this:

<input id="mytext" type="text" onchange="RemoveFormat(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.

Steve Wortham
Thank you very much for your detailed answer.
CindyH
+1  A: 

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):

<span id="mySpan" onclick="test(this);">TEST</span>
<script type="text/javascript">
function test(el) {
    // el is a reference to an element, you can use it directly
    el.style.display = 'none';

    // to demonstrate that the element you retrieve from gEBI 
    // with el.id is the same as your original reference
    var elAgain = document.getElementById(el.id);
    alert(elAgain === el); // "true"
}
</script>

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:

<span id="mySpan" onclick="test(this, 'myDiv');">TEST</span>
<div id="myDiv" style="display:none;">DIV REVEALED</div>
<script type="text/javascript">
function test(el, otherElId) {
    // el is a reference to an element, you can use it directly
    el.style.display = 'none';
    // otherElId is a string containing the id of
    // the element you want to control
    var theDiv = document.getElementById(otherElId);
    theDiv.style.display = '';
}
</script>

Note that there is rarely (if ever) a reason to do something like the following:

<span id="mySpan" onclick="test('mySpan');">TEST</span>
<script type="text/javascript">
function test(theElId) {
    var el = document.getElementById(theElId);
    el.style.display = 'none';
}
</script>

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.

Grant Wagner
Thank you very much, not only for the answer but for educating me for the more-to-come.
CindyH
A: 

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.

Thom Smith
Thanks - I appreciate the clarification.
CindyH
A: 

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.

olliej