Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to get javascript to enable / disable radio buttons

Posted on 2011-03-06
6
Medium Priority
?
632 Views
Last Modified: 2012-06-27
So below are the HTML and the javascript where all I'm trying to do is enable / disable (and clear) a second set of radio buttons based on yes / no for the first ones.  I know the code runs, as I've put alerts in to make sure the test of the first ' yes / no' is being found.   (the javascript is tied to the radio buttons via the code behind:

        ClientScript.RegisterClientScriptInclude("activateImagingStage", "activateImagingStage.js")
        Imaging_Yes.Attributes.Add("onclick", "activateImagingStage();")
        Imaging_No.Attributes.Add("onclick", "activateImagingStage();")

In IE8, the second set of radio buttons gets activated / deactivated based on the first, but the second set don't get cleared when they are disabled.

In FF, nothing gets enabled / disabled.

Seems like it should be so simple...
<asp:TableRow ID="TableRow28" runat="server">
                                    <asp:TableCell  runat="server" ColumnSpan="4">22.  Did you perform imaging studies?
                                       
                                    </asp:TableCell>
                                    <asp:TableCell  CssClass="answer" runat="server">
                                        <asp:RadioButton ID="Imaging_Yes" GroupName="imaging" runat="server" />
                                    </asp:TableCell>
                                    <asp:TableCell  CssClass="answer" runat="server">
                                        <asp:RadioButton ID="Imaging_No" GroupName="imaging" runat="server" />
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow ID="Question23a" runat="server" Enabled="false" >
                                    <asp:TableCell  runat="server" ColumnSpan="4">23.  What stage was the diagnostic imaging ordered?
                                   
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow ID="Question23b" runat="server"  >
                                    <asp:TableCell></asp:TableCell>
                                    <asp:TableCell ID="tc8b" runat="server" ColumnSpan="4">
                                        <asp:RadioButtonList ID="Imaging_Stage" runat="server">
                                            <asp:ListItem  Value="1" runat="server" Text="Stage 0 or 1A" />
                                            <asp:ListItem  Value="2" runat="server" Text="Other" />
                                        </asp:RadioButtonList>
                                    </asp:TableCell>
                                </asp:TableRow>

Open in new window

function activateImagingStage(r) {

    var Imaging_Stage = document.getElementsByName("<%= Imaging_Stage.ClientID %>".replace(/_/g, '$'));

		if ((document.getElementById('Imaging_Yes').checked) ) {
		    document.getElementById('Question23a').disabled = false;
		    document.getElementById('Question23b').disabled = false;
		    for (var i = 0; i < Imaging_Stage.length; i++) {
		        Imaging_Stage[i].disabled = false ;
		    }
 	        }
		
 	    else {
 	        for (var i = 0; i < Imaging_Stage.length; i++) {
 	            Imaging_Stage[i].disabled = true;
 	            Imaging_Stage[i].checked = false;
		    }
    		    document.getElementById('Question23a').disabled = true;
    		    document.getElementById('Question23b').disabled = true;
 	    }
}

Open in new window

0
Comment
Question by:d2fox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35049772
Hi, You have to use the onClientClick method of a button for calling javascript in asp.net.
0
 

Author Comment

by:d2fox
ID: 35049894
Thanks.  Ok, so I just tried that and nothing happens when I use

        ClientScript.RegisterClientScriptInclude("activateImagingStage", "activateImagingStage.js")
        Imaging_Yes.Attributes.Add("onclientclick", "activateImagingStage();")
        Imaging_No.Attributes.Add("onclientclick", "activateImagingStage();")

At least with what I have above, I know I'm getting to the javascript function.
0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 35050476
Try the javascript below...
function activateImagingStage(r) {  
  
    var Imaging_Stage = document.getElementsByName("<%= Imaging_Stage.ClientID %>".replace(/_/g, '$'));  
  
                if ((document.getElementById('Imaging_Yes').checked) ) {  
                    document.getElementById('Question23a').disabled = false;  
                    document.getElementById('Question23b').disabled = false;  
                    for (var i = 0; i < Imaging_Stage.length; i++) {  
                        Imaging_Stage[i].disabled = false ;  
                    }  
                }  
                  
            else {  
                for (var i = 0; i < Imaging_Stage.length; i++) {  
                 Imaging_Stage[i].checked = false;                     
                 Imaging_Stage[i].disabled = true;                     
                    }  
                    document.getElementById('Question23a').disabled = true;  
                    document.getElementById('Question23b').disabled = true;  
            }  
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:d2fox
ID: 35051312
that works in FF but not IE.

hmmm...
0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 35051474
0
 
LVL 4

Accepted Solution

by:
GeoffHarper earned 2000 total points
ID: 35056575
I haven't verified that your JavaScript works correctly, and I'm not an ASP.NET guy, but I know radio buttons act funny if you put only an ID on them; they need a NAME too.  See your code (modified) below.

-Geoff


<asp:TableRow ID="TableRow28" NAME="TableRow28" runat="server">
    <asp:TableCell  runat="server" ColumnSpan="4">22.  Did you perform imaging studies?
       
    </asp:TableCell>
    <asp:TableCell  CssClass="answer" runat="server">
	<asp:RadioButton ID="Imaging_Yes" NAME="Imaging_Yes" GroupName="imaging" runat="server" />
    </asp:TableCell>
    <asp:TableCell  CssClass="answer" runat="server">
	<asp:RadioButton ID="Imaging_No" NAME="Imaging_No" GroupName="imaging" runat="server" />
    </asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="Question23a" NAME="Question23a" runat="server" Enabled="false" >
    <asp:TableCell  runat="server" ColumnSpan="4">23.  What stage was the diagnostic imaging ordered?
   
    </asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="Question23b" NAME="Question23b" runat="server"  >
    <asp:TableCell></asp:TableCell>
    <asp:TableCell ID="tc8b" NAME="tc8b" runat="server" ColumnSpan="4">
	<asp:RadioButtonList ID="Imaging_Stage" NAME="Imaging_Stage" runat="server">
	    <asp:ListItem  Value="1" runat="server" Text="Stage 0 or 1A" />
	    <asp:ListItem  Value="2" runat="server" Text="Other" />
	</asp:RadioButtonList>
    </asp:TableCell>
</asp:TableRow>

Open in new window

0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question