Risho
Risho

Reputation: 2647

Uncheck check box when dropdown selection changes - not working

In a MVC website, how do you make a check box unchecked if a dropdown list has changed value? I'm not sure if MVC behaves differently form traditional asp.Net application but what I coded does not work.

In default.aspx I have a JavaScript function:

function UncheckFooBox() {
   var isChecked = document.getElementById("cbxFoo").checked;
        if (isChecked)
            document.getElementById("cbxFoo").checked = true;

then

<input type="checkbox" name="cbxFoo" runat="server" 
     checked="checked" id="cbxFoo" />Foo

then below all this I have the dropdown list

<select Name="FooChecker" onchange="UncheckFooBox()" >
    <option value="1" >Yes</option>
    <option value="2" >No</option>
    <option value="0"> </option>
</select>

So the outcome should be that if I change the selection of the FooChecker, the cbxFoo is checked, it will uncheck it on the client side, can't have a post back at this point just yet. - thanks!

Please note that jQuery is not really an option at this time. (I know, I should learn it...)

Upvotes: 0

Views: 1302

Answers (1)

Kaz-LA
Kaz-LA

Reputation: 226

Try this:

function UncheckFooBox() {
     var elem = document.getElementById("cbxFoo");
     var isChecked = elem.getAttribute('checked');
    if(isChecked) 
        elem.removeAttribute('checked');
    }

Upvotes: 1

Related Questions