By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,717 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,717 IT Pros & Developers. It's quick & easy.

Macro substitution in Javascript

100+
P: 110
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
Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027
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

100+
P: 110
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
Expert Mod 15k+
P: 16,027
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
Expert Mod 5K+
P: 8,639
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.