How do I add an alert condition to a For loop?



Hello Everyone,

Could anyone give me a hand with this? I am having a hard time adding code to a For loop to get the result I need.

Here is the situation: I am developing a very long form full of conditional inputs. Instead of adding code to each individual field that needs to be shown/hidden upon user input I would rather have a function that looks for fields that start with an arbitrary prefix in their names ("H-").

For example:

  • Question: "Do you have a bachelor degree?"
  • Answers: Yes | No (radio buttons)
    • Yes: call function that shows questions about that bachelor degree
    • No: call function that hides questions about that bachelor degree

Of course the form is a lot more complex than the example above. Anyway, I have a fully functional function that find the fields in question and shows and hides them perfectly.

// Hide/Show fields based on field name prefix (substring)

// sectionName = parent subform where to find the fields (search range)

// endPosition = integer, position of last character to be searched in field name prefix (initial position is always zero)

// prefixString = string that identifies fields affected by this function

function hideFields(sectionName,endPosition,prefixString) {

    var oFields = sectionName.nodes;  

    var nNodesLength = oFields.length; 


    for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) { 

         if (oFields.item(nNodeCount).name.substring(0,endPosition) == prefixString) { 

oFields.item(nNodeCount).presence = "hidden";

       } // close if

    } // close for

// close function

However I need to add functionality to the script above. Before those contextual fields are hidden from the user I would like the function to check if the field contains any values. If the field to be hidden has values the function has to alert the user w/ a message box and proceed only if the user clicks ok to “yes, hide the fields - I know that will delete whatever input in them”.

Note: oFields could be a (sub)form. If the oFields turns out to be a subform the script would need to check each form field (child) under that subform (parent) for values before it prompts the user. I also would like the user to be prompted only once – for instance if we are deleting values from 10 fields in a subform we need just one prompt for all inputs under that given subform. Make sense?

I hit a wall and am not sure where I am making mistakes. I have the code to add the alert condition but when I add that to the function above it the function breaks. Any ideas are very welcome.



Accepted Solutions (0)

Answers (0)