Link to home
Start Free TrialLog in
Avatar of anwarmir
anwarmir

asked on

Javascript : how to detect form elements that have changed.

Hi, I am totally new to javascript and need some help in debugging some code. What I need to do is only loop through fields that have been changed by the user. So I am guessing is there a property I can utilise for form.landingpages where there has been a change by the user. Any help would be appreciated:


for (var x=0; x < form.landingpages.length; x++){                        
//do not process the hidden field
if (form.elements['landingpages'][x].type != 'hidden') {                  
// keep a track of vanities to be created
currentVanity = eval("form.vanity_" + form.elements['landingpages'][x].value + ".value.toUpperCase()");
if (currentVanity.length > 0) {
aVanitiesToAdd.push(currentVanity);
                        }                                                      
Avatar of frindo
frindo

Well if all of the form fields are text fields then you can loop through and check to see if the value of the field is not equal to the default (if the field is empty it would be like this):

if(formfield.value != "") {
//do stuff
}

This can also work with almost every other type of form field by just knowing the default option and checking accordingly. (In the case of a checkbox you could see if it was checked it it defaults to unchecked)
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just using onChange attribute of the input field, every input field has onChange event, just add an event listener to each input field, the following will upper case the input (if I not misunderstand your provided code):
  <script type="text/javascript">
   function upperMe(el){
   alert(el);
     if ((el.tagName.toLowerCase() == "input") && (el.type.toLowerCase() == 'text')){
         // Make sure we get the desired element <input type="text">
         el.value = el.value.toUpperCase();
     }
   }
</script>
<form>
<input type="text" onchange="upperMe(this)">
...
</form>
So you want to only save the changed fields.
reading your code, I assume you have a bunch of fields all called landingpages, some hidden
I also assume you have for values entered some fields called vanity_something where the user can enter something

I think I would code it like this (assuming the above is correct - next time post more code, please):

for (var i=0; i < form.landingpages.length; i++){                        
  //do not process the hidden field
  if (form.elements['landingpages'][x].type == 'hidden') continue;                  
  var page = form.elements['landingpages'][i].value;
// keep a track of vanities to be created
  var vanity = form.elements["vanity_" +page].value;
  if (vanity.length > 0 && vanity !=form.elements["vanity_" +page].defaultValue)  {
    aVanitiesToAdd.push(vanity.toUpperCase());
  }                                        
}

Michel
Avatar of anwarmir

ASKER

Thanks Michel.
Here is the full code I think :)
      var aVanityNames = new Array(#UCase(QuotedValueList(rsVanities.vcVanityName))#);
            var aReservedVanityNames = new Array(#UCase(QuotedValueList(Session.stCampaign.stSettings.rsReservedVanities.Name))#);

            for (var x=0; x < form.landingpages.length; x++){                                                                  
                  //do not process the hidden field
                  if (form.elements['landingpages'][x].type != 'hidden') {                  
                        // keep a track of vanities to be created
                        currentVanity = eval("form.vanity_" + form.elements['landingpages'][x].value + ".value.toUpperCase()");                                    
                        if (currentVanity.length > 0) {
                              aVanitiesToAdd.push(currentVanity);
                        }                                                      
      
                        // Check for illegal characters in the vanity name
                        if (currentVanity.match(/[^A-Za-z0-9]/) != null) {
                              dVanityNameError = 1;                        
                              vanityErrorMessage += '* The ' + currentVanity + ' Vanity Name contains illegal characters' + '\n';                                                            
                        }
                        
                        // check vanity does not already exist in db
                        for (var i=0; i<aVanityNames.length; i++) {
                              if ( currentVanity == aVanityNames[i]) {
                                    dVanityNameError = 1;
                                    vanityErrorMessage += '* The ' + currentVanity + ' Vanity Name is already in use' + '\n';
                                    break;
                              }
                        }                                                                  
                  //do not process the hidden field
                  if (form.elements['landingpages'][x].type != 'hidden') {                  
                        // keep a track of vanities to be created
                        currentVanity = eval("form.vanity_" + form.elements['landingpages'][x].value + ".value.toUpperCase()");                                    
                        if (currentVanity.length > 0) {
                              aVanitiesToAdd.push(currentVanity);
                        }                                                      
      
                        // Check for illegal characters in the vanity name
                        if (currentVanity.match(/[^A-Za-z0-9]/) != null) {
                              dVanityNameError = 1;                        
                              vanityErrorMessage += '* The ' + currentVanity + ' Vanity Name contains illegal characters' + '\n';                                                            
                        }
                        
                        // check vanity does not already exist in db
                        for (var i=0; i<aVanityNames.length; i++) {
                              if ( currentVanity == aVanityNames[i]) {
                                    dVanityNameError = 1;
                                    vanityErrorMessage += '* The ' + currentVanity + ' Vanity Name is already in use' + '\n';
                                    break;
                              }
                        }
Actually I was looking for the html of the form
Is the # thingy Cold fusion or something?

And I see the script twice...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin