Reputation: 15
Here's the code for a simple page I have that is not working:
<head>
<script src="js/jquery-1.7.min.js"></script>
<script type="text/javascript">
<!--
$("input[type=radio]").on('click', function(){
if ($('#radio1').is(':checked')){
$("#grid_9.omega").slideDown("slow");
} else {
$("#grid_9.omega").slideUp("slow");
}
});
//-->
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label>
<input type="radio" name="RadioGroup1" value="radio1" id="radio1" />
Radio 1</label>
<br />
<label>
<input type="radio" name="RadioGroup1" value="radio2" id="radio2" />
Radio 2</label>
<br />
<label>
<input type="radio" name="RadioGroup1" value="radio3" id="radio3" />
Radio 3</label>
<br />
</p>
</form>
<div id="grid_9" class="omega" style="display:none">show me when Radio 1 is chosen</div>
</body>
My goal is to use jquery to display div "grid_9.omega" when "radio1" is selected, but have that div hidden when "radio1" isn't selected.
Advice?
Upvotes: 0
Views: 2300
Reputation: 4254
Try this: jsFiddle
<script type="text/javascript">
$(function() {
$("input[type=radio]").on('click', function(){
if ($('#radio1').is(':checked')){
$("#grid_9.omega").slideDown("slow");
} else {
$("#grid_9.omega").slideUp("slow");
}
});
});
</script>
<form id="form1" name="form1" method="post" action="">
<p>
<label>
<input type="radio" name="RadioGroup1" value="radio1" id="radio1" />
Radio 1</label>
<br />
<label>
<input type="radio" name="RadioGroup1" value="radio2" id="radio2" />
Radio 2</label>
<br />
<label>
<input type="radio" name="RadioGroup1" value="radio3" id="radio3" />
Radio 3</label>
<br />
</p>
</form>
<div id="grid_9" class="omega" style="display:none">show me when Radio 1 is chosen</div>
All I changed from your code was I added the document ready in the beginning of the script.
Upvotes: 2
Reputation: 87073
Simply you can do:
$("input[type=radio]").on('click', function(){
if(this.id == 'radio1')
$("#grid_9.omega").slideDown("slow");
else
$("#grid_9.omega").slideUp("slow");
});
If your radio is dynamic i.e. append to DOM after page load then you need delegate event handler like following:
$('body').on('click', 'input[type=radio]', function() {
if(this.id == 'radio1')
$("#grid_9.omega").slideDown("slow");
else
$("#grid_9.omega").slideUp("slow");
});
Instead of input[type=radio]
you can use only :radio
as selector. i.e
$(':radio').on('click', ...)
or
$('body').on('click', ':radio',..)
In case of delegate event it would be better to use any other static element instead of body
. Suppose, if your form1
is Static to DOM i.e belong to DOM at time of page load then instead of
$('body').on('click', 'input[type=radio]', function() {..})
you should use
$('#form1').on('click', 'input[type=radio]', function() {..})
Place all of your code within
$(document).ready(function() {
// your code
});
is short
$(function() {
// your code
});
Upvotes: 3
Reputation: 68400
Try with this
<script type="text/javascript">
<!--
$(function(){
$(document).on('change','input:radio', function(){
if ($('#radio1').is(':checked')){
$("#grid_9.omega").slideDown("slow");
} else {
$("#grid_9.omega").slideUp("slow");
}
});
}
//-->
</script>
Upvotes: 1