Solved

jQuery How to Show/Hide input fields using radio butttons

Posted on 2011-02-17
5
942 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

911 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

17 Experts available now in Live!

Get 1:1 Help Now