Solved

jQuery How to Show/Hide input fields using radio butttons

Posted on 2011-02-17
5
934 Views
Last Modified: 2012-05-11
Greetings All;

I'm having a problem  Showing/Hiding input fields using radio butttons. If Radio1 is clicked, Input1 should display and input2 should be hidden.  If Radio2 is clicked, Input2 should display and input1 should be hidden. On load both input1 and input2 should be hidden.

My code is listed below. What an I missing?

Thanks again for your time and consideration.
´*•.¸(`*•.¸?¸.•*´)¸.•*´
?*°Doris 42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Show Enable - Hide Disable Based on radio Selection</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.9.custom.min.js"></script>
<script type="text/javascript">
$("input:radio[@name=radioone]:checked").click(function() {
        // display/enable the input with ID "inputa1"
        $("#inputa1").show();
        $("#inputa1").removeAttr('disabled');
        $("#inputa1"').removeAttr("readonly");
        $("inputa1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
        $("inputb1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
        // hide/disable the input with ID "inputb1"
        $("#inputb1").hide();
        $("#inputb1").attr('disabled', true);
        $("#inputb1").attr('readonly', true);
        $("inputa1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
        $("inputb1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
}); 
$("input:radio[@name=radiotwo]:checked").click(function() {
        // display/enable the input with ID "inputb1"
       $("#inputb1").show();
       $("#inputb1").removeAttr('disabled');
       $("#inputb1"').removeAttr("readonly"); 
       $("inputb1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
       $("inputa1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
        // hide/disable the input with ID "inputa1"
        $("#inputa1").hide();
        $("#inputa1").attr('disabled', true);
        $("#inputa1").attr('readonly', true);
        $("inputb1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
        $("inputa1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
});
// on load hide/disable inputs with the ID "inputb1" & "inputb2"
  $("#inputa1").hide();
  $("#inputa1").attr('disabled', true);
  $("#inputa1").attr('readonly', true);
  $("inputa1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
  $("#inputb1").hide();
  $("#inputb1").attr('disabled', true);
  $("#inputb1").attr('readonly', true); 
  $("inputb1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
});
</script>
</head>
<body>
  <label><input type="radio" name="radioone" /> RADIO1 </label>
  <label><input type="radio" name="radiotwo" /> RADIO2</label>
 <br><br>
<input type="text" id="inputa1" size="45" value="INPUT A1">
<input type="text" id="inputb1" size="45" value="INPUT B1">
</body>
</html>

Open in new window

0
Comment
Question by:DMackallii
5 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34916425

There were a few problems, does this get any closer to your requirements:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26828369.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://www.appelsiini.net/projects/lazyload/jquery.lazyload.js" type="text/javascript"></script>
<script type="text/javascript">

jQuery(document).ready( function () {

$("input:radio[name=radioone]").click(function() {
        // display/enable the input with ID "inputa1"
        $("#inputa1").show();
        $("#inputa1").removeAttr('disabled');
        $("#inputa1").removeAttr("readonly");
        $("#inputa1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
        $("#inputb1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
        // hide/disable the input with ID "inputb1"
        $("#inputb1").hide();
        $("#inputb1").attr('disabled', true);
        $("#inputb1").attr('readonly', true);
        $("#inputa1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
        $("#inputb1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
}); 
$("input:radio[name=radiotwo]").click(function() {
        // display/enable the input with ID "inputb1"
       $("#inputb1").show();
       $("#inputb1").removeAttr('disabled');
       $("#inputb1").removeAttr("readonly"); 
       $("#inputb1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
       $("#inputa1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
        // hide/disable the input with ID "inputa1"
        $("#inputa1").hide();
        $("#inputa1").attr('disabled', true);
        $("#inputa1").attr('readonly', true);
        $("#inputb1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
        $("#inputa1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
});
// on load hide/disable inputs with the ID "inputb1" & "inputb2"
  $("#inputa1").hide();
  $("#inputa1").attr('disabled', true);
  $("#inputa1").attr('readonly', true);
  $("#inputa1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
  $("#inputb1").hide();
  $("#inputb1").attr('disabled', true);
  $("#inputb1").attr('readonly', true); 
  $("#inputb1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
});

</script>
</head>
<body>

  <label><input type="radio" name="radioone" /> RADIO1 </label>
  <label><input type="radio" name="radiotwo" /> RADIO2</label>
 <br><br>
<input type="text" id="inputa1" size="45" value="INPUT A1">
<input type="text" id="inputb1" size="45" value="INPUT B1">

</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34916426
We set the value of each radio button with the id of its associated textbox

to select the radio buttons not checked : input[type='radio'][name='myradio']:not(:checked)
to select the radio buttons checked : input[type='radio'][name='myradio']:checked

we can simplify the code if you've only two radio button of course.

Test page :




<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Show Enable - Hide Disable Based on radio Selection</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$("input[type='radio'][name='myradio']").click(function() {
			$("input[type='radio'][name='myradio']:not(:checked)").each(function() {$("#" + $(this).attr("value")).hide(); });
			$("input[type='radio'][name='myradio']:checked").each(function() { $("#" + $(this).attr("value")).show(); });
		});
	});
</script>
</head>
<body>
  <label><input type="radio" name="myradio" value="inputa1" /> RADIO1 </label>
  <label><input type="radio" name="myradio" value="inputb1" /> RADIO2</label>
 <br><br>
<input type="text" id="inputa1" size="45" value="INPUT A1" style="display:none">
<input type="text" id="inputb1" size="45" value="INPUT B1" style="display:none">
</body>
</html>

Open in new window

0
 
LVL 3

Expert Comment

by:vnsmanian2006
ID: 34916479
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Show Enable - Hide Disable Based on radio Selection</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>

<script type="text/javascript">



// when DOM is ready.
$(document).ready(function(){
// on load hide/disable inputs with the ID "inputb1" & "inputb2"
  $("#inputa1").hide();
  $("#inputa1").attr('disabled', true);
  $("#inputa1").attr('readonly', true);
  $("inputa1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
  $("#inputb1").hide();
  $("#inputb1").attr('disabled', true);
  $("#inputb1").attr('readonly', true);
  $("inputb1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
 
 
 
  $(".opt").click(function() {
         console.log($(this).val());
        if ($(this).val() == 'option1') {
               
                // display/enable the input with ID "inputa1"
                $("#inputa1").show();
                $("#inputa1").removeAttr('disabled');
                $("#inputa1").removeAttr("readonly");
                $("inputa1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
                $("inputb1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
               
                // hide/disable the input with ID "inputb1"
                $("#inputb1").hide();
                $("#inputb1").attr('disabled', true);
                $("#inputb1").attr('readonly', true);
                $("inputa1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
                $("inputb1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});

        } else {
               
                // display/enable the input with ID "inputb1"
                $("#inputb1").show();
                $("#inputb1").removeAttr('disabled');
                $("#inputb1").removeAttr("readonly");
                $("inputb1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
                $("inputa1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
                // hide/disable the input with ID "inputa1"
                $("#inputa1").hide();
                $("#inputa1").attr('disabled', true);
                $("#inputa1").attr('readonly', true);
                $("inputb1").css({"background-color":"ffffff","color":"black","border":"1px solid black"});
                $("inputa1").css({"background-color":"ffffff","color":"white","border":"0px solid white"});
       
                   
        }
});
});
</script>
</head>
<body>
  <label><input value="option1" type="radio" name="options" class="opt" /> RADIO1 </label>
  <label><input value="option2" type="radio" name="options" class="opt"  /> RADIO2</label>
 <br><br>
<input type="text" id="inputa1" size="45" value="INPUT A1">
<input type="text" id="inputb1" size="45" value="INPUT B1">
</body>
</html>
0
 

Author Closing Comment

by:DMackallii
ID: 34922375
Greetings All;
Thank you for the prompt responses. After testing each solution I'm going to award the points to leakim971.  leakim971 you've done it again!
T H A N K S!
´*•.¸(`*•.¸?¸.•*´)¸.•*´
?*°Doris 42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34922424
Thanks a lot for the points! Have a nice end of week and a lot of fun!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now