Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts

    remove class from form field

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

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

    Code:
    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
    Dale Ellis
    __________________

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,576
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    How hard will you kick yourself??

    Code:
    	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>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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
    Dale Ellis
    __________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •