1

I have 2 check boxes where only one or none may be checked. Since I can't do a postback I tried this with Javascript. The Javascript finds the element (tested it with an alert). But the value won't change.

Any Idea how I can do this with Javascript?

The Javascript:

function mrcAndNbbFilterChanged(mrcOrNbb)
        {
            alert("er in");
          if(mrcOrNbb == 0)
          { 
            document.getElementById("ctl00_contentHolder_cb_mrcFilter").checked=true; 
            document.getElementById("ctl00_contentHolder_cbNoBackBilling").checked=false; 
            alert(document.getElementById("ctl00_contentHolder_cbNoBackBilling"));
            alert("0");
          }
          else
          {
            if(mrcOrNbb == 1)
            {
                alert("1");
                document.getElementById("cb_mrcFilter").checked=false; 
                document.getElementById("cbNoBackBilling").checked=true; 
            }
          }
        }

The ASP code:

<asp:CheckBox ID="cb_mrcFilter" runat="server" Text="Only MRC" OnClick="mrcAndNbbFilterChanged(0)" /> 
<asp:CheckBox ID="cbNoBackBilling" runat="server" Text="No back billing" OnClick="mrcAndNbbFilterChanged(1)"  />    
5
  • This should work. Can you create a fiddle to replicate? Commented May 22, 2013 at 9:55
  • 1
    why not just use the checkbox list (or radio button list my be better as that limits the user to only having one selected), also is there a reason you can't do a post back? Commented May 22, 2013 at 9:57
  • @ØHankyPankyØ I'll try to do this. But I won't be surprised if it works in Fiddle Commented May 22, 2013 at 9:59
  • @jgok222 Checkbox list could be an option. Doesn't need a radio button always need one value to be selected? (it also looks different). There will be functions running when I do a postback, this is why I can't do this. Commented May 22, 2013 at 10:01
  • as @JamesDonnolly pointed, you should check the HTML code generated by ASP, to be sure that you are using the correct ID ;) Commented May 22, 2013 at 10:03

1 Answer 1

4

ASP.NET applies that ctl00_-esque string to IDs when using ASP controls to ensure they are unique. You can get the ASP-modified ID value using:

document.getElementById("<%= cb_mrcFilter.ClientID %>").checked=false;
document.getElementById("<%= cbNoBackBilling.ClientID %>").checked=true;

Also, as a side note, you can use else if { ... }, rather than else { if { ... } } when only dealing with one alternative:

if(mrcOrNbb == 0) {
    ...
}
else if(mrcOrNbb == 1) {
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the else if advice, but I'm wondering if an else would be enough on this case ;)
It was indeed the id, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.