JavaScript Form Function

Hi,

Total noob here with javascript . I’m trying to alter a function. This is the one that is currently there and works.

function hideStateField(theForm) {
    theForm.state.disabled = true;
    theForm.state.className = 'hiddenField';
    theForm.state.setAttribute('className', 'hiddenField');
    document.getElementById("stateLabel").className = 'hiddenField';
    document.getElementById("stateLabel").setAttribute('className', 'hiddenField');
    document.getElementById("stateText").className = 'hiddenField';
    document.getElementById("stateText").setAttribute('className', 'hiddenField');
    document.getElementById("stateBreak").className = 'hiddenField';
    document.getElementById("stateBreak").setAttribute('className', 'hiddenField');
}

I want to make it more generic so its not specific to the “state” field. So I’m changing the function name to reflect that and adding a 2nd parameter. Then I’m trying to use that 2nd parameter as a variable in place of where we see “state”.

function hideAddressField(theForm,theField) {
    theForm.theField.disabled = true;
    theForm.theField.className = 'hiddenField';
    theForm.theField.setAttribute('className', 'hiddenField');
    document.getElementById(theField+"Label").className = 'hiddenField';
    document.getElementById(theField+"Label").setAttribute('className', 'hiddenField');
    document.getElementById(theField+"Text").className = 'hiddenField';
    document.getElementById(theField+"Text").setAttribute('className', 'hiddenField');
    document.getElementById(theField+"Break").className = 'hiddenField';
    document.getElementById(theField+"Break").setAttribute('className', 'hiddenField');
}

I tested it simply with “state” as the 2nd variable to be sure it worked… and it didn’t. I keep getting “Uncaught TypeError: Cannot set property ‘disabled’ of undefined”. I’m sure its a syntax error. My call to this function is:

hideAddressField(theForm,state);

The form’s name is “theForm” as well so I figured the variable “theForm” was being assigned a value of “theForm” while the variable “theField” was being assigned the value of “state” and the two functions should be equivelant. Obviously not.

Thanks and Regards,
Lavanya Sreepada.

Hello there,

I moved your question to its own topic because you were asking a question related to your own code for a challenge and were not answering the OP of the other thread. It is always best to create your own thread for you specific question(s).

Also, I have edited your post to remove the link.

Thank you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.