Solved

jQuery How to Show/Hide input fields using radio butttons

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Progress 4gl Rounding Function 6 61
Save data in two Database, Asp 2 71
Javascript Error 10 24
How to change normal array form to associative in javascript 1 24
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…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

752 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