Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery How to Show/Hide input fields using radio butttons

Posted on 2011-02-17
5
Medium Priority
?
996 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
[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
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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

721 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