471,887 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Macro substitution in Javascript

110 100+
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
  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.     }
  16.     if(isEmpty(firstBlankField)!=true){
  17.         document.getElementById(firstBlankField).focus();
  18.     }
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 1880
16,027 Expert Mod 8TB
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
110 100+
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
16,027 Expert Mod 8TB
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
8,658 Expert Mod 8TB
wasn't isEmpty() expecting an element as input?
Apr 9 '09 #5

Post your reply

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

Similar topics

6 posts views Thread by Raghuveer Pallikonda | last post: by
8 posts views Thread by Siemel Naran | last post: by
1 post views Thread by me | last post: by
7 posts views Thread by Newbie_sw2003 | last post: by
14 posts views Thread by Malcolm | last post: by
1 post views Thread by Rodolfo | last post: by
4 posts views Thread by Don | last post: by
3 posts views Thread by XHengDF | last post: by
37 posts views Thread by junky_fellow | last post: by
reply views Thread by zermasroor | last post: by

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.