MaVVamaldo
MaVVamaldo

Reputation: 2535

problems in changing label color with javascript

I have to do a very basic thing with javascript: A form field is triggered by a check box; if the checkbox is not checked the field must be disabled and its label must be grey.

EDIT: here's the generated HTML code from the following JSP code (of course the c:choose tag outputs the second condition):

    <div class="form">
        <form id="myForm" name="myForm" action="settings" method="post">
            <table>
                <tr>
                    <td><label for="compatibilityMode">comp. mode</label></td>
                    <td><input id="compatibilityMode" name="compatibilityMode" onclick="toggle()" name="compatibilityMode" class="checkbox" type="checkbox" value="true"/><input type="hidden" name="_compatibilityMode" value="on"/>
                </tr>
                <tr>                        
                    <td><label for="localbaseuri" id="localbaseurilabel" name="localbaseurilabel" class="labeldisabled">local base URI</label></td>
                    <td><input id="localbaseuri" name="localApplicationBaseURI" name="localbaseuri" class="text" disabled="disabled" type="text" value="" size="40"/></td>                                          
                    <td></td>
                </tr>
            </table>
            <div class="form-row">
                <input type="submit" value="submit"/>
            </div>
        </form>
    </div>

So, here's my code:

    <div class="form">
        <form:form id="myForm" name="myForm" method="post" action="settings" commandName="settings">
            <table>
                <tr>
                    <td><form:label for="compatibilityMode" path="compatibilityMode">comp. mode</form:label></td>
                    <td><form:checkbox id="compatibilityMode" class="checkbox" path="compatibilityMode" name="compatibilityMode" onclick="toggle()"/>
                </tr>
                <tr>
                <c:choose>
                    <c:when test="${settings.compatibilityMode}">
                        <td><form:label id="localbaseurilabel" name="localbaseurilabel" class="labelenabled" for="localbaseuri" path="localApplicationBaseURI">local base URI</form:label></td>
                        <td><form:input id ="localbaseuri" class="text" path="localApplicationBaseURI" size="40" name="localbaseuri" disabled="false"/></td>                        
                    </c:when>
                    <c:otherwise>
                        <td><form:label id="localbaseurilabel" name="localbaseurilabel" class="labeldisabled" for="localbaseuri" path="localApplicationBaseURI">local base URI</form:label></td>
                        <td><form:input id ="localbaseuri" class="text" path="localApplicationBaseURI" size="40" name="localbaseuri" disabled="true"/></td>
                    </c:otherwise>
                </c:choose>                     
                    <td><form:errors path="localApplicationBaseURI" cssClass="error"/></td>
                </tr>
            </table>
            <div class="form-row">
                <input type="submit" value="submit"/>
            </div>
        </form:form>
    </div>

and the script

<script language="JavaScript">
function toggle()
{
    if(document.myForm.compatibilityMode.checked == false)
        {
            document.myForm.localbaseuri.disabled = true;
            document.myForm.localbaseurilabel.className = "labeldisabled";

        }
    else
        {
            document.myForm.localbaseuri.disabled = false;
            document.myForm.localbaseurilabel.className = "labelenabled";
        }

}
</script>

with this code when the checkbox is checked the field is activated properly (and viceversa) but the filed doesn't change its style. I tried to set the color attribute as well but with no result.

  document.myForm.localbaseurilabel.style.color = "grey";

What am I missing?

Upvotes: 0

Views: 5067

Answers (1)

Vitalii Petrychuk
Vitalii Petrychuk

Reputation: 14255

document.getElementById('localbaseurilabel').style.color = "grey";

Upvotes: 1

Related Questions