Solved

jQuery How to Show/Hide input fields using radio butttons

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

828 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