473,511 Members | 16,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Macro substitution in Javascript

110 New Member
I have previously asked a question with the same title line but my situation and purpsse are different this time. I mean to be creating a function to go back over a form, identify blank required fields, change their borders to red then go back and give focus to the first one.

Expand|Select|Wrap|Line Numbers
  1. function checkUnitFields(x){
  2.     var num=x; // item number
  3.     var firstBlankField=null; // variable to be used to move focus to first field that needs attention
  4.     var secondBlankField=null; // variable to be used to move focus to second field that needs attention
  5.     var thirdBlankField=null; // variable to be used to move focus to third field that needs attention
  6.     var fourthBlankField=null; // variable to be used to move focus to fourth field that need attention
  7.     var fifthBlankField=null; // variable to be used to move focus to fifth field that needs attendtion
  8.  
  9.     if(isEmpty(document.getElementById('modelYear'+num).value)==true){
  10.         document.getElementById('modelYear'+num).style.border="1px solid #FF0000";
  11.         if(firstBlankField==null){
  12.             firstBlankField="modelYear";
  13.         }
  14.     }
  15.  
  16.     if(isEmpty(firstBlankField)!=true){
  17.         document.getElementById(firstBlankField).focus();
  18.     }
  19.  
There are actually five possible fields that could be blank but I only included the first here becauxe it has no problem identifying the blank fields and making the borders red. The problem is with the last if() statement where I am trying to return focus() to the first blank field. Since nothing is in parenthesis I am uncertain how to distinguish that I want the value of variable 'firstBlankField' substitued for the name of the variable in my getElementById() statement. Essentially I need to do a macro substitution and I don't know how. Can anybody offer a solution?
Apr 7 '09 #1
4 2103
acoder
16,027 Recognized Expert Moderator MVP
So you mean modelYear is the name of a variable?

You could improve this code and make it generic by looping over elements rather than using fixed variable names.
Apr 7 '09 #2
andersond
110 New Member
No. ModelYear is the name of the field. The variable is named firstBlankField. I want the macro to substitute 'modelYear' for 'firstBlankField' in the last if() statement. And so on.

I don't understand what you mean by 'looping over elements rather than using fixed variable names. How would I do that?
Apr 7 '09 #3
acoder
16,027 Recognized Expert Moderator MVP
If modelYear was the ID, then your code would be correct. If it's the name, you could use the form elements collection:
Expand|Select|Wrap|Line Numbers
  1. document.forms[formName].elements[firstBlankField]...
By looping over the elements, I meant something like:
Expand|Select|Wrap|Line Numbers
  1. var len = document.forms[formName].elements.length;
  2. for (...
or using document.getElementsByTagName("input") and then checking the type/name etc.
Apr 9 '09 #4
Dormilich
8,658 Recognized Expert Moderator Expert
wasn't isEmpty() expecting an element as input?
Apr 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2240
by: Raghuveer Pallikonda | last post by:
Hi, I am trying to stub out debug log messages in my application, if the logging subsystem is not enabled.. For e.g a invocation #define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg so...
8
5334
by: Siemel Naran | last post by:
#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__, __LINE__, #x); } MSVC7 gives an error: "error C2014: preprocessor command must start as first nonwhite space".
1
1611
by: me | last post by:
Hi guys I want to insert a load of pieces of data into a map The map has an std::string representing a field name as the key, and the value is a struct with 2 members - the field length and a...
7
23528
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
14
2043
by: Malcolm | last post by:
Hi, I have the following which fails with "disagreement in number of macro arguments" when compiling with Imagecraft ICCAVR. Has anyone got any ideas - its not vital but would make the code a...
1
5414
by: Rodolfo | last post by:
Hello, there's another languages that can do a macro substitution, how can I do this in Csharp. This is an example of what I want to do Dataset ds = new Dataset; string a = "ds"; DataSet...
4
6898
by: Don | last post by:
I think "macro substitution" is the correct term for what I want to do, but, to be sure, here is a description of what I'd like to know is possible: I want to be able to create a create an object...
3
2168
by: XHengDF | last post by:
recent days. i am confuse on the macros in boost. could anyone tell me the replacement rules of the macros in c++, or somelinks about the macros in boost thanks
37
8933
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to...
0
7242
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
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
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
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
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...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.