Reputation: 1759
can someone give me help on a simple jQuery code that when clicked different radio bottuon then display the different content.
http://jsfiddle.net/AXsVY/
HTML
<label class="radio inline">
<input id="up_radio" type="radio" name="optionsRadios" value="update" checked>
Update
</label>
<label class="radio inline">
<input id="ov_radio" type="radio" name="optionsRadios" value="overwritten">
Overwritten
</label>
<p id="cont">
sth. here
</p>
JavaScript
$("#up_radio").click(function(){
if ($("#up_radio").is(":checked")) {
//change to "show update"
$("#cont").value = "show update";
} else if ($("#ov_radio").is(":checked")) {
// change to "show overwritten"
}
});
Upvotes: 1
Views: 18808
Reputation: 28409
Use on change not on click. Click triggers change, but the user could also tab across and hit an arrow key. It will always change
.
$('input[name="optionsRadios"]').on('change', function(){
if ($(this).val()=='update') {
//change to "show update"
$("#cont").text("show update");
} else {
$("#cont").text("show Overwritten");
}
});
Also, the correct syntax to set a value is $("#something").val("the value");
but in this case #cont is a div, so you need to use .text()
or .html()
.
Upvotes: 13
Reputation: 5993
Solved, based on your approach.. There are more optimizations you could do, but I left it mostly as-is:
http://jsfiddle.net/digitalextremist/mqrHs/
Here's the code outside a fiddle:
$(".radio input[type='radio']").on( 'click', function(){
if ($("#up_radio").is(":checked")) {
$("#cont").html( "show update" );
} else if ($("#ov_radio").is(":checked")) {
$("#cont").html( "show overwritten" );
}
});
Upvotes: 3
Reputation: 40318
$("input:radio[name=optionsRadios]").click(function() {
var value = $(this).val();
if(value="update")
$("#cont").text("show update");
else
$("#cont").text("show update other");
});
Upvotes: 0