Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Radio Buttons to Disable a Select Box

Posted on 2005-04-01
5
Medium Priority
?
335 Views
Last Modified: 2008-02-26
Hey there,

I've got another easy one. In the code below, I've got two radio buttons, and one of the radio buttons has a select box "associated" with it. Here's what I need to happen.

When the first radio button is selected, I need the select box to become disabled (and have nothing selected, maybe that happens by default once it's disabled?). When the second radio buttton is selected the selct box should become active again and the first item should be selected.

I think this is all pretty straightforward if you look at the HTML:

<tr><td>
<form name="channelType">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" rowspan="2"><input type="radio" name="whichType" id="existing_channel" value=""></td>
<td valign="top"><label for="existing_channel"><b>Radio Button 1</b></label></td>
</tr>

<tr><td valign="top" colspan="2"><img src="../images/spacer.gif" width="1" height="16" border="0"></td></tr>

<tr>
<td valign="top" rowspan="3"><input type="radio" name="whichType" id="new_channel" value="" checked></td>
<td><label for="new_channel"><b>Radio Button 2</b></label></td>
</tr>

<tr><td>
<select name="picture" size="5" style="width: 160px">
<option value="" selected>Item 1</option>
<option value="">Item 1</option>
<option value="">Item 2</option>
<option value="">Item 3</option>
<option value="">Item 4</option>
</select>
</td></tr>

</table>
</form>
</td></tr>

BTW, the page that this will go on also has the following JavaScript function which figures out which radio button is selected and when the submit button is selected and takes the user to the appropriate page. I only bring this up to make sure that everything plays nicely together.  Thanks!

<script type="text/javascript">
<!--
function goToChannel(){
    if(document.channelType.elements['whichType'][0].checked == true){
          top.location.href = 'message_rss.html';
    }
    else if(document.channelType.elements['whichType'][1].checked == true){
          top.location.href = 'message_text.html';
    }
    else{
    }
}
// -->
</script>
0
Comment
Question by:oldschwinn
  • 3
5 Comments
 
LVL 25

Accepted Solution

by:
archrajan earned 100 total points
ID: 13684525
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<tr><td>
<form name="channelType">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" rowspan="2"><input type="radio" name="whichType" id="existing_channel" value="" ONCLICK = "if(this.checked)document.channelType.picture.disabled = true;"></td>
<td valign="top"><label for="existing_channel"><b>Radio Button 1</b></label></td>
</tr>

<tr><td valign="top" colspan="2"><img src="../images/spacer.gif" width="1" height="16" border="0"></td></tr>

<tr>
<td valign="top" rowspan="3"><input type="radio" name="whichType" id="new_channel" value="" checked ONCLICK = "if(this.checked)document.channelType.picture.disabled = false;"></td>
<td><label for="new_channel"><b>Radio Button 2</b></label></td>
</tr>

<tr><td>
<select name="picture" size="5" style="width: 160px">
<option value="" selected>Item 1</option>
<option value="">Item 1</option>
<option value="">Item 2</option>
<option value="">Item 3</option>
<option value="">Item 4</option>
</select>
</td></tr>

</table>
</form>
</td></tr>
</BODY>
</HTML>
0
 
LVL 32

Assisted Solution

by:Batalf
Batalf earned 100 total points
ID: 13684545
Example:

<html>
<body>
<script>
var savedSelection;
function disableListBox(formObj){
    var obj = formObj.picture;
    savedSelection = obj.selectedIndex;
    obj.selectedIndex=-1;
    obj.disabled=true    
   
}

function enableListBox(formObj){
    var obj = formObj.picture;
    if(obj.selectedIndex=savedSelection);
    obj.disabled=false    
}
</script>
<table>
<tr><td>
<form name="channelType">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" rowspan="2"><input type="radio" onclick="disableListBox(this.form)" name="whichType" id="existing_channel" value=""></td>
<td valign="top"><label for="existing_channel"><b>Radio Button 1</b></label></td>
</tr>

<tr><td valign="top" colspan="2"><img src="../images/spacer.gif" width="1" height="16" border="0"></td></tr>

<tr>
<td valign="top" rowspan="3"><input type="radio" onclick="enableListBox(this.form)" name="whichType" id="new_channel" value="" checked></td>
<td><label for="new_channel"><b>Radio Button 2</b></label></td>
</tr>

<tr><td>
<select name="picture" size="5" style="width: 160px">
<option value="" selected>Item 1</option>
<option value="">Item 1</option>
<option value="">Item 2</option>
<option value="">Item 3</option>
<option value="">Item 4</option>
</select>
</td></tr>

</table>
</form>
</td></tr>
</table>
</body>
0
 
LVL 25

Expert Comment

by:archrajan
ID: 13684579
changed the 2 lines again for selecting the first option
<tr>
<td valign="top" rowspan="2"><input type="radio" name="whichType" id="existing_channel" value="" ONCLICK = "if(this.checked){document.channelType.picture.selectedIndex = -1;document.channelType.picture.disabled = true;}"></td>
<td valign="top"><label for="existing_channel"><b>Radio Button 1</b></label></td>
</tr>

<tr><td valign="top" colspan="2"><img src="../images/spacer.gif" width="1" height="16" border="0"></td></tr>

<tr>
<td valign="top" rowspan="3"><input type="radio" name="whichType" id="new_channel" value="" checked ONCLICK = "if(this.checked){document.channelType.picture.disabled = false;document.channelType.picture.selectedIndex = 0}"></td>
<td><label for="new_channel"><b>Radio Button 2</b></label></td>
</tr>
0
 

Author Comment

by:oldschwinn
ID: 13684630
Hey Guys,

Both solutions are awesome! I wanted to give you both 50 points, but the system wouldn't let me, so I had to split them. I'll create another question and give you each 25 more points for your help.

Thanks again!
0
 
LVL 25

Expert Comment

by:archrajan
ID: 13684639
Thats great!
thanks for the points !
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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

564 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