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['landingpag es'][x].ty pe != 'hidden') {
// keep a track of vanities to be created
currentVanity = eval("form.vanity_" + form.elements['landingpage s'][x].val ue + ".value.toUpperCase()");
if (currentVanity.length > 0) {
aVanitiesToAdd.push(curren tVanity);
}
for (var x=0; x < form.landingpages.length; x++){
//do not process the hidden field
if (form.elements['landingpag
// keep a track of vanities to be created
currentVanity = eval("form.vanity_" + form.elements['landingpage
if (currentVanity.length > 0) {
aVanitiesToAdd.push(curren
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
<script type="text/javascript">
function upperMe(el){
alert(el);
if ((el.tagName.toLowerCase()
// 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['landingpag es'][x].ty pe == 'hidden') continue;
var page = form.elements['landingpage s'][i].val ue;
// 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 .toUpperCa se());
}
}
Michel
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['landingpag
var page = form.elements['landingpage
// 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
}
}
Michel
ASKER
Thanks Michel.
Here is the full code I think :)
var aVanityNames = new Array(#UCase(QuotedValueLi st(rsVanit ies.vcVani tyName))#) ;
var aReservedVanityNames = new Array(#UCase(QuotedValueLi st(Session .stCampaig n.stSettin gs.rsReser vedVanitie s.Name))#) ;
for (var x=0; x < form.landingpages.length; x++){
//do not process the hidden field
if (form.elements['landingpag es'][x].ty pe != 'hidden') {
// keep a track of vanities to be created
currentVanity = eval("form.vanity_" + form.elements['landingpage s'][x].val ue + ".value.toUpperCase()");
if (currentVanity.length > 0) {
aVanitiesToAdd.push(curren tVanity);
}
// 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['landingpag es'][x].ty pe != 'hidden') {
// keep a track of vanities to be created
currentVanity = eval("form.vanity_" + form.elements['landingpage s'][x].val ue + ".value.toUpperCase()");
if (currentVanity.length > 0) {
aVanitiesToAdd.push(curren tVanity);
}
// 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;
}
}
Here is the full code I think :)
var aVanityNames = new Array(#UCase(QuotedValueLi
var aReservedVanityNames = new Array(#UCase(QuotedValueLi
for (var x=0; x < form.landingpages.length; x++){
//do not process the hidden field
if (form.elements['landingpag
// keep a track of vanities to be created
currentVanity = eval("form.vanity_" + form.elements['landingpage
if (currentVanity.length > 0) {
aVanitiesToAdd.push(curren
}
// Check for illegal characters in the vanity name
if (currentVanity.match(/[^A-
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['landingpag
// keep a track of vanities to be created
currentVanity = eval("form.vanity_" + form.elements['landingpage
if (currentVanity.length > 0) {
aVanitiesToAdd.push(curren
}
// Check for illegal characters in the vanity name
if (currentVanity.match(/[^A-
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...
Is the # thingy Cold fusion or something?
And I see the script twice...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Forced accept.
Computer101
EE Admin
Computer101
EE Admin
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)