...

View Full Version : remove class from form field



ellisd5
06-18-2010, 10:02 AM
Hi all, I have a couple of functions, an add error function, if some validation is not met I add a message to a array holding error messages and mark the field red, I have no problem with that piece of code...


function addError(errorArray, formObj, message) {
errorArray[errorArray.length] = message;
if (formObj.options != undefined) {
formObj.parentNode.className = "error-field";
} else {
formObj.className = "error-field";
}
}

However I have an issue with the 2nd function, it takes in 2 variables, an id for a div which is displaying the error messages, and the form object, the issue I have is removing the error class. for some reason obj.className = '' fails to remove the class, getting the same object using getElementById() (name and id are the same) however if I construct the removing piece of js code as a String and execute it using eval it works...


function clearErrors(errorDivId, vForm) {
var errorDiv = document.getElementById(errorDivId);
errorDiv.innerHTML = "";
errorDiv.style.display = "none";

// remove red boxes
for (i=0; i<vForm.elements.length; i++) {
var obj = vForm.elements[i];
if (obj.type != "button" && obj.type != "hidden") {
if (obj.options != undefined) {
if (obj.parentNode.className == "error-field") {
obj.parentNode.className = "";
}
} else {
if (obj.className == "error-field") {
alert(obj.name);
obj.className == "";
alert(obj.className);
obj = document.getElementById(obj.name);
alert(obj);
obj.className == "";
alert(obj.className);
var jsExecuteCommand = "vForm." + obj.name + ".className = '';";
alert(jsExecuteCommand);
eval(jsExecuteCommand);
alert(obj.className);
}
}
}
}
}

I am so baffled? I have no idea why it won't work can anybody please explain what is happening please? Although I have a work around, I have been banging my head to get to this workaround and I just don't understand why the obj.className = ''; doesn't work.

Thanks,
Dale

Old Pedant
06-18-2010, 08:51 PM
How hard will you kick yourself??



if (obj.className == "error-field") {
alert(obj.name);
obj.className == "";
alert(obj.className);
obj = document.getElementById(obj.name);
alert(obj);
obj.className == "";
alert(obj.className);
var jsExecuteCommand = "vForm." + obj.name + ".className = '';";

So those lines with the == evaluates to false and JS says..."okay, it's false, so what? he doesn't do anything with the false value. oh, well, too bad..."

The line with the = works just fine, of course.

<grin>USUALLY, the problem with == and = is the other way around. Congratulations.</grin>

ellisd5
06-19-2010, 10:58 AM
Noooooooooooooooooooooooooooooooo !

I feel quite a plonker, as you say its usually the other way around that gets missed, assignment in if statements.

Oh well, I guess that's the benefit of a 2nd pair of eyes.

Thanks for the help, that explains a lot

Cheers,
Dale



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum