I'm going to create 3 radio buttons for user to select. Once, user click on that button, it will show user the description of that radio button that user selected.
For example
- if user select first radio button ==> it will show the description under that
radio button
- if user select second radio button ==> it will show the description under that
radio button
- if user select third radio button ==> it will show the description under that
radio button
My code
<p><input type='radio' name='content_type' value='1' /> This is content A of request</p>
<p><input type='radio' name='content_type' value='2' /> This is content B of request</p>
<p><input type='radio' name='content_type' value='3' /> This is content C of request</p>
<div id='show'></div>
Javascript
$(document).ready(function(){
$('#content_type').on('change', function(){
var n = $(this).val();
switch(n)
{
case '1':
document.getElementById('#show').innerHTML="1st radio button";
break;
case '2':
document.getElementById('#show').innerHTML="2nd radio button";
break;
case '3':
document.getElementById('#show').innerHTML="3rd radio button";
break;
}
});
My code above didn't work. Can anyone one help me to show this problem?
Thank in advance
I'm going to create 3 radio buttons for user to select. Once, user click on that button, it will show user the description of that radio button that user selected.
For example
- if user select first radio button ==> it will show the description under that
radio button
- if user select second radio button ==> it will show the description under that
radio button
- if user select third radio button ==> it will show the description under that
radio button
My code
<p><input type='radio' name='content_type' value='1' /> This is content A of request</p>
<p><input type='radio' name='content_type' value='2' /> This is content B of request</p>
<p><input type='radio' name='content_type' value='3' /> This is content C of request</p>
<div id='show'></div>
Javascript
$(document).ready(function(){
$('#content_type').on('change', function(){
var n = $(this).val();
switch(n)
{
case '1':
document.getElementById('#show').innerHTML="1st radio button";
break;
case '2':
document.getElementById('#show').innerHTML="2nd radio button";
break;
case '3':
document.getElementById('#show').innerHTML="3rd radio button";
break;
}
});
My code above didn't work. Can anyone one help me to show this problem?
Thank in advance
Share Improve this question asked Oct 31, 2013 at 8:22 user2738520user2738520 1,3914 gold badges13 silver badges17 bronze badges4 Answers
Reset to default 4You are incorrectly using jQuery selectors. Also you need to close the $(document).ready(). Right code:
$(document).ready(function(){
$('input[name=content_type]').on('change', function(){
var n = $(this).val();
switch(n)
{
case '1':
$('#show').html("1st radio button");
break;
case '2':
$('#show').html("2nd radio button");
break;
case '3':
$('#show').html("3rd radio button");
break;
}
});
});
JSfiddle
Your selector is wrong.
1) Instead of #content_type
, you should use input[name=content_type]
2) Also, remove the #
from getElementById:
document.getElementById('#show')
should be
document.getElementById('show')
or change it to use jQuery:
$('#show').html("1st radio button");
Here is the modified html ....i simply added a span to your messages
<p><input type='radio' class="rad" name='content_type' value='1' /> <span >This is content A of request</span></p>
<p><input type='radio' class="rad" name='content_type' value='2' /> <span>This is content B of request</span></p>
<p><input type='radio' name='content_type' value='3' /> <span>This is content C of request</span></p>
<div id='show'></div>
And here is the jQuery
$(document).ready(function(){
$('span').hide();
$('input[name="content_type"]').on('change',function(){
$('span').hide();
$(this).next().show();
});
});
And here is the demo
use this code:
$('input[name=content_type]').on('change', function(){
OR
$('input[type=radio]').on('change', function(){
#content_type
means you are selecting an element that has the id named content_type. and you don't have such any element.
After that your JS code will be:
$(document).ready(function(){
$('input[type=radio]').change(function(){
var n = $(this).val();
switch(n)
{
case '1':
$('#show').html("1st radio button");
break;
case '2':
$('#show').html("2nd radio button");
break;
case '3':
$('#show').html("3rd radio button");
break;
}
});
});