Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vars as shortcuts

Status
Not open for further replies.

jez

Programmer
Joined
Apr 24, 2001
Messages
370
Location
VN
Hey there,

I have seen in many books and on several tutorial websites the practice of using a variable to save typing e.g.

var d = document.all.mylayer;

then later to use the whole dot'd notation simply putting;-

d.visibility = "hide";

A great idea but after using javascript on and off for a year now, I can still say this has NEVER worked for me.

I'm sure there must be something i am missing, but whenever i try to get it right i get a message saying that my shortcut var (in this case d) is null or undefined.
Usually I try for a while and then just type it all long-hand.

Anyone who can tell me what exactly i need to do to get this working will really help me out, not to mention save my typing.

Many thanks


Jez

:-)
 
Have you got an example of where this didn't work? I've used the technique before and it seems to work fine for me too. The only time it doesn't work that way is if part of the object name is a string in a variable. Then you probably need to say, e.g.:
Code:
var fld = eval("document.myform.field"+fldnum);
fld.value = "someval";
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
And it may also depend what browser you are coding for. For example, the document.all object is IE specific, and will not return a valid object for NS.

For the new generation of browsers, the World Wide Web Consortium's Document Object Model (DOM) provides a more browser neutral approach to coding. Example:

Code:
var node = document.getElementById( 'abc' );
var nodeList = document.getElementsByTagName( 'select' );
if( node && node.hasAttributes() ) {
    var namedNodeMap = node.attributes;
}

More information on the DOM can be found at:

Cheers, NEIL
 
TSDragon,
I think you may have sorted this for me.
As an example of it not working I would give this;-

var tex = document.formname.textareaname;

where both 'formname' and 'textareaname' are strings defined in the HTML for these form elements.

If i understand you correctly, this is the rule i didn't know, (hence always getting it wrong).

So the following should work, right?

var b = document.form[0];

b.textareaname.value = "it works";

###############################

Thanks again,


Jez

oh, and P.S. to Neil, I was only using that as an example of a object i would shortcut, and I generally try and be cross-browser friendly....so was interested to see the stuff you put about dodging browser specifics.



:-) :-) :-)
 
Actually, if you've got html like the following:
Code:
<form name=&quot;myform&quot; ...>
<input type=&quot;text&quot; name=&quot;mytext&quot; ...>
then either of these should work:
Code:
var ta = document.forms[0].mytext;
- or -
var ta = document.myform.mytext;
- and then -
ta.value = &quot;some text&quot;;
In this case the names are strings in the tags, but they become the object names, so there's no problem.

The only time you need to use eval is if you actually want to incorporate a value into the name of the object at runtime. For example if you have:
Code:
<form name=&quot;myform&quot; ...>
<input type=&quot;text&quot; name=&quot;mytext1&quot; ...>
<input type=&quot;text&quot; name=&quot;mytext2&quot; ...>
<input type=&quot;text&quot; name=&quot;mytext3&quot; ...>
Then you could loop thru the text fields like this:
Code:
for ( var x = 1; x < 4; x++ ) {
   var txt = eval(&quot;document.myform.mytext&quot;+x);
   txt.value = &quot;text field &quot; + x; 
}
See how the eval lets you incorporate the value of x into the object name?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top