0

I know this is probably a simple fix but I can't seem to figure out the issue here...

I am trying to disable withinthepast text box when RadioButton1 is clicked.

<asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="DateTimeQuery" OnClick="javascript:TimeRangeClickEvent()"/>
<asp:TextBox ID="withinthepast" runat="server" Text="1"></asp:TextBox>

<script>
        function TimeRangeClickEvent()
        {
            var radio1 = document.getElementById('<%=RadioButton1.ClientID%>').checked;                

            if (radio1 == true)
            {
                var within = document.getElementById('<%=withinthepast.ClientID%>').enabled = false;
            }
        }
    </script>

Any advice?

3
  • Did you try adding a few console.log statements to monitor what is going on in TimeRangeClickEvent? You could add: console.log('radio1: ' + radio1); before the "if" to see if radio1 is really true. The console can be viewed with F12 in most browsers (AFAIK). And same question as Alex: why the "within" variable? Commented Feb 25, 2016 at 16:01
  • I just tested with an alert and it does in fact alert "true" if checked and "false" if not. <script> function TimeRangeClickEvent() { var radio1 = document.getElementById("<%=RadioButton1.ClientID %>").checked; if (radio1 == true) { alert(radio1); } else { alert(radio1); } } </script> Commented Feb 25, 2016 at 16:11
  • But if I try to disable the text box I still get the correct alerts and nothing happens to the text box being enabled/disabled. Commented Feb 25, 2016 at 16:16

2 Answers 2

1

The problem is probably with the "enabled" property. You can try this:

To enable a control: ctl.removeAttribute("disabled")

To disable a control: ctl.setAttribute("disabled", "disabled")

Sign up to request clarification or add additional context in comments.

1 Comment

Yep. Thank you that worked! Much appreciated. Cheers!
1

try this

if (radio1.checked == true)
        {
            var within = document.getElementById('<%=withinthepast.ClientID%>').enabled = false;
        }

you can add attributes clientIdMode = static

2 Comments

Nope sorry. Didn't have any effect. I shouldn't have to put the OnClick event on the textbox as well right? Also, if i do cllientIDMode = static isn't there a chance I could end up with controls sharing the same ID as mentioned here stackoverflow.com/questions/6057490/…
Any other suggestions anyone? I'm at a loss here. Nothing I have tried seems to be working...

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.