I wrote this jsfiddle based largely on the example found here. The example is over 2 years old so I was wondering if there is an easier way to check the value of a radio button than gobs of if, elseif, else
statements. My values are going to be disabled, both, footer, header
so with only four not too bad. I'm just trying to learn how to write better Javascript.
Html:
<input type="radio" name="rdio" value="1" checked="checked" /> 1
<input type="radio" name="rdio" value="2" /> 2
<input type="radio" name="rdio" value="3" /> 3
<div id="div_1"> Div 1 Content</div>
<div id="div_2"> Div 2 Content</div>
<div id="div_3"> Div 3 Content</div>
Javascript:
$('#div_1,#div_2,#div_3').css('display','none');
$("input[name=rdio]").change(function() {
if ($("input[name=rdio]:checked").val() == '1') {
$('#div_1').show();
$('#div_2,#div_3').hide();
}
else if ($("input[name='rdio']:checked").val() == '2') {
$('#div_2').show();
$('#div_1,#div_3').hide();
}
else {
$('#div_3').show();
$('#div_2,#div_1').hide();
}
});
JSFiddle to play around with: http://www.jsfiddle.net/bs54j/1/
UPDATE: To revise question
So I posted this before realizing what I would actually be doing so here is an updated fiddle with the actual code I'm using: http://jsfiddle.net/BandonRandon/BsupQ/3/
New Html:
<h2> What to show? </h2>
<input type="radio" name="show_custom_header_footer" value="disabled" checked="checked" /> Disabled
<input type="radio" name="show_custom_header_footer" value="both" /> Both
<input type="radio" checked name="show_custom_header_footer" value="header" /> Header
<input type="radio" name="show_custom_header_footer" value="footer" /> Footer
<div id="header_footer_options">
This is the main content div of all the options
<p id="custom_header">Custom Header:<br/> <textarea rows="2" cols="100" name="custom_header"> Custom Header Code</textarea></p>
<p id="custom_footer">Custom Footer:<br/> <textarea rows="2" cols="100" name="custom_footer">Custom Footer Code</textarea></p></div>
New Javascript:
//show hide custom header/footer depending on settings
$('#header_footer_options').hide();
//check status on change
$("input[name='show_custom_header_footer']").change(function() {
if ($("input[name='show_custom_header_footer']:checked").val() == 'disabled') {
$('#header_footer_options').fadeOut(500);
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'both') {
$('#header_footer_options').fadeIn();
$('#custom_header,#custom_footer').fadeIn();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'header') {
$('#header_footer_options').fadeIn();
$('#custom_header').fadeIn();
$('#custom_footer').hide();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'footer') {
$('#header_footer_options').fadeIn();
$('#custom_footer').fadeIn();
$('#custom_header').hide();
}
else {
$('#header_footer_options').hide();
}
});
//check status on page load
if ($("input[name='show_custom_header_footer']:checked").val() == 'disabled') {
$('#header_footer_options').hide();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'both') {
$('#header_footer_options').show();
$('#custom_header,#custom_footer').show();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'header') {
$('#header_footer_options').show();
$('#custom_header').show();
$('#custom_footer').hide();
}
else if ($("input[name='show_custom_header_footer']:checked").val() == 'footer') {
$('#header_footer_options').show();
$('#custom_footer').show();
$('#custom_header').hide();
}
else {
$('#header_footer_options').hide();
}
Probably the biggest thing that needs to be refactored is the show/hide on load I feel like I have a ton of extra crap code in there. I'm not looking for a handout just a suggestion on how to make this work better/faster/stronger.