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?

Tim
  • 952
  • 3
  • 12
  • 31
  • 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? – ConnorsFan Feb 25 '16 at 16:01
  • I just tested with an alert and it does in fact alert "true" if checked and "false" if not. – Tim Feb 25 '16 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. – Tim Feb 25 '16 at 16:16

2 Answers2

1

try this

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

you can add attributes clientIdMode = static

regex
  • 101
  • 1
  • 9
  • 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 http://stackoverflow.com/questions/6057490/is-there-any-drawback-to-set-clientidmode-static-on-every-object-set-on-main – Tim Feb 25 '16 at 15:42
  • Any other suggestions anyone? I'm at a loss here. Nothing I have tried seems to be working... – Tim Feb 25 '16 at 15:45
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")

ConnorsFan
  • 70,558
  • 13
  • 122
  • 146