Solved

Show textarea if radio button selected

Posted on 2011-02-17
5
693 Views
Last Modified: 2012-05-11
I want to show a text box if a radio button is selected.  I have the following radio buttons:

<input type="radio" name="resolved" id="Yes" value="Y" />
<input type="radio" name="resolved" id="No" value="N" />

And the following code which is not working:

<script type="text/javascript">
<!--
$('input[name=resolved]').change(function() {
if($(this).val() == "Y") {
$('#resolution').show();
} else {
 $('#resolution').hide();    
 }
 });
 //-->
</script>

Any help appreciated!
0
Comment
Question by:peterbrowne
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34922866
using :

		$("input[type='radio'][name='resolved']").click(function() {
			if( $(this).attr("value") == "N" ) {
				$("#ta").show();
			}
			else {
				$("#ta").hide();
			}
		});

Open in new window


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='resolved']").click(function() {
			if( $(this).attr("value") == "N" ) {
				$("#ta").show();
			}
			else {
				$("#ta").hide();
			}
		});
	});
</script>
</head>
<body>
<input type="radio" name="resolved" id="Yes" value="Y" />Yes<br />
<input type="radio" name="resolved" id="No" value="N" checked />No<br />
<textarea id="ta" cols="20" rows="5"></textarea>
</body>
</html>

Open in new window

0
 

Author Comment

by:peterbrowne
ID: 34922917
Thanks, how do I have the text area hidden by default.  I've changed your code as follows:

<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='resolved']").click(function() {
                  if( $(this).attr("value") == "Y" ) {
                        $("#resolution").show();
                  }
                  else {
                        $("#resolution").hide();
                  }
            });
      });
</script>
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34922925
with jquery :

      $(document).ready(function() {
            $("#ta").hide();
            $("input[type='radio'][name='resolved']").click(function() {
...

Open in new window


with style :)  

<textarea id="ta" cols="20" rows="5" style="display:none"></textarea>

Open in new window

0
 

Author Comment

by:peterbrowne
ID: 34922943
Well that was easy...

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
      $(document).ready(function() {
            $("#resolution").hide();
            $("input[type='radio'][name='resolved']").click(function() {
                  if( $(this).attr("value") == "Y" ) {
                        $("#resolution").show();
                  }
                  else {
                        $("#resolution").hide();
                  }
            });
      });
</script>
0
 

Author Closing Comment

by:peterbrowne
ID: 34922945
Many thanks!!
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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 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