473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Shortnening my JS DOM references

Hi All

In one of my JS files, I have a lot of what appears to be unnecessary and
repetitive text, eg:

document.order.cardnumberbox1.value = '';
document.order.cardnumberbox2.value = '';
document.order.cardnumberbox3.value = '';
document.order.cardnumberbox4.value = '';
document.order.cardnumberbox5.value = '';
document.order.cardholder.value = '';
document.order.expirymonth.value = '01';
document.order.expiryyear.value = '2003';
document.order.startmonth.value = 'N/A';
document.order.startyear.value = 'N/A';
document.order.issuenumber.value = 'N/A';
document.order.securitycode.value = '';

To try to shorten the lines and my overall text size I thought I would do
the following:

var doc = 'document.order';

doc.cardnumberbox1.value = '';
doc.cardnumberbox2.value = '';
doc.cardnumberbox3.value = '';
etc... etc..

But it doesn't work.

If poss, could you please advise me as to where I'm gonig wrong.

Thanks Robbie
Jul 23 '05 #1
4 950
"Astra" <in**@NoEmail.com> skrev i meddelandet
news:10*************@corp.supernews.com...
Hi All

In one of my JS files, I have a lot of what appears to be unnecessary and
repetitive text, eg:

document.order.cardnumberbox1.value = '';
document.order.cardnumberbox2.value = '';
document.order.cardnumberbox3.value = '';
document.order.cardnumberbox4.value = '';
document.order.cardnumberbox5.value = '';
document.order.cardholder.value = '';
document.order.expirymonth.value = '01';
document.order.expiryyear.value = '2003';
document.order.startmonth.value = 'N/A';
document.order.startyear.value = 'N/A';
document.order.issuenumber.value = 'N/A';
document.order.securitycode.value = '';

To try to shorten the lines and my overall text size I thought I would do
the following:

var doc = 'document.order';

<snip>

var doc=document.order;

You want a reference to a form, not a concatenated string.

Joakim Braun
Jul 23 '05 #2
var doc = 'document.order'; >> > var doc = document.order;


without quotes, it should work

Regards,

Tom
Jul 23 '05 #3
On Thu, 2 Dec 2004 16:01:03 -0000, Astra <in**@NoEmail.com> wrote:

[snip]
var doc = 'document.order';

doc.cardnumberbox1.value = '';


As others have pointed out, the code above creates a string, doc, not a
reference to the form. Instead, use:

var form = document.forms['order'],
elem = form.elements;

/* which could be shortened to
* var elem = document.forms['order'].elements;
*/

elem['cardnumberbox1'].value = '';

The forms and elements collections are the most cross-browser approach to
referencing forms and their controls.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Thanks Guys

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsid2a4x5x13kvk@atlantis...
On Thu, 2 Dec 2004 16:01:03 -0000, Astra <in**@NoEmail.com> wrote:

[snip]
var doc = 'document.order';

doc.cardnumberbox1.value = '';


As others have pointed out, the code above creates a string, doc, not a
reference to the form. Instead, use:

var form = document.forms['order'],
elem = form.elements;

/* which could be shortened to
* var elem = document.forms['order'].elements;
*/

elem['cardnumberbox1'].value = '';

The forms and elements collections are the most cross-browser approach to
referencing forms and their controls.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
22
by: xmp333 | last post by:
Hi All, I am trying to hide my JavaScript source. The method I chose was to keep all the important source in a password protected folder, and then use a SRC="folder/script.js" to include it...
33
by: JKop | last post by:
I understand variables/objects and pointer variables perfectly: int X = 5; int* pX = &X; *pX = 4; int** ppX = &pX:
2
by: S. van Beek | last post by:
Dear reader, For removing a reference in the VBA reference form I receive from Doug Steele the following code: ........... References.Remove refCurr
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
30
by: jeremygetsmail | last post by:
I've got an adp (Metrix.adp) with a reference to another adp (InteractSQL.adp). InteractSQL sits on a server, and is refered to by all of the clients (Metrix), which sit on the client machines...
3
by: DonJefe | last post by:
Does anyone have experience using project->project references in large solutions? What are the plus/minuses that you have found? Currently, we are using the binary assembly references for our...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
3
by: CenturionX | last post by:
Hello everybody: I'd like to know what references in my vba code are used or not. I work in a code made by another person previously, i founded to many references and i believe that someones...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.