Tim
Tim

Reputation: 952

Pass asp.net controls as params to javascript function

I am trying to get a javascript function to work properly with my asp.net controls. I have two radio buttons and three textboxes.

When the first radio button is clicked I would like to disable the third textbox and when the second radio button is clicked I would like to disable the third text box. See the attachment for a screenshot.

I have tested that the function works by adding "OnClick" to the first radio button and setting AN ALERT WHICH IS WORKING.

However, I am not sure how to pass the controls in to the function to have access to them. Anyone know how to do this?

Screenshot enter image description here

ASP.NET

<asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="DateTimeQuery" OnClick="TimeRangeClickEvent()"/>
<asp:TextBox ID="startdatetext" Enabled="true" runat="server" MaxLength="10"></asp:TextBox>
<asp:TextBox ID="enddatetext" Enabled="true" runat="server" MaxLength="10"></asp:TextBox>

<asp:RadioButton ID="RadioButton2" runat="server" GroupName="DateTimeQuery" OnClick="TimeRangeClickEvent() />
<asp:TextBox ID="withinthepast" Enabled="true" runat="server" Text="1"></asp:TextBox>

Javascript

<script>
        function TimeRangeClickEvent(How do I pass asp.net controls here)
        {
            alert('Working');            
        }
    </script>

Upvotes: 0

Views: 164

Answers (1)

Andrei
Andrei

Reputation: 44550

You should use ClientID control properties to get IDs of HTML elements and then work with them from Javascript. More info.

In Javascript:

var radio1 = document.getElementById('<%=RadioButton1.ClientID%>');

When you have all your HTML elements you just need to handle necessary change/clicked/selected events in javascript to make it work.

Upvotes: 1

Related Questions