Macro substitution in Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andersond
    New Member
    • Feb 2007
    • 110

    #1

    Macro substitution in Javascript

    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.

    Code:
    function checkUnitFields(x){
    	var num=x; // item number
    	var firstBlankField=null; // variable to be used to move focus to first field that needs attention
    	var secondBlankField=null; // variable to be used to move focus to second field that needs attention
    	var thirdBlankField=null; // variable to be used to move focus to third field that needs attention
    	var fourthBlankField=null; // variable to be used to move focus to fourth field that need attention
    	var fifthBlankField=null; // variable to be used to move focus to fifth field that needs attendtion
    	
    	if(isEmpty(document.getElementById('modelYear'+num).value)==true){
    		document.getElementById('modelYear'+num).style.border="1px solid #FF0000";
    		if(firstBlankField==null){
    			firstBlankField="modelYear";
    		}
    	}
    
    	if(isEmpty(firstBlankField)!=true){
    		document.getElementById(firstBlankField).focus();
    	}
    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 'firstBlankFiel d' 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?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    • andersond
      New Member
      • Feb 2007
      • 110

      #3
      No. ModelYear is the name of the field. The variable is named firstBlankField . I want the macro to substitute 'modelYear' for 'firstBlankFiel d' 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?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        If modelYear was the ID, then your code would be correct. If it's the name, you could use the form elements collection:
        Code:
        document.forms[formName].elements[firstBlankField]...
        By looping over the elements, I meant something like:
        Code:
        var len = document.forms[formName].elements.length;
        for (...
        or using document.getEle mentsByTagName( "input") and then checking the type/name etc.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          wasn't isEmpty() expecting an element as input?

          Comment

          Working...