Reputation: 881
jsFiddle here.
I'm a novice at Javascript and having trouble following the documentation at the iCheck page. I've followed the answer to what appears to be a very relevant StackOverflow question here but cannot get an alert to pop up displaying the value I've selected. Can anyone point me in the right direction, please?
HTML
<p>Are you sure you want to have your details removed from our marketing list?</p>
<div id="unsubscribe_radio">
<ul>
<li>
<input type="radio" name="iCheck" value="yes">
<label>Yes</label>
</li>
<li>
<input type="radio" name="iCheck" value="no">
<label>No</label>
</li>
</ul>
</div>
Javascript
$(document).ready(function () {
$('input').iCheck({
radioClass: 'iradio_flat-orange'
});
$("input:radio[name=iCheck]").click(function () {
var value = $(this).val();
alert("You clicked " + value);
});
});
Upvotes: 16
Views: 43926
Reputation: 1688
For radio buttons and current version of iCheck:
<div class="row">
<div class="col-lg-1">
<div class="form-group">
<label>
<input type="radio" name="my-radio" class="flat-green my-radio" value="true"> Yes
</label>
</div>
</div>
<div class="col-lg-1">
<div class="form-group">
<label>
<input type="radio" name="my-radio" class="flat-red my-radio" value="false"> No
</label>
</div>
</div>
</div>
JS:
var isChecked = $("input[name=my-radio]:checked").val();
It will return undefined if no option is selected, true or false if selected.
Upvotes: 0
Reputation: 1435
this works for me... try it
HTML: <input type="checkbox" id=checkbox" name="checkbox" />
jQuery: $( "#checkbox" ).prop("checked");
Upvotes: 1
Reputation: 9612
JS:-
$(document).ready(function () {
$('input').iCheck({
radioClass: 'iradio_flat-orange'
});
$('input').on('ifClicked', function (event) {
var value = $(this).val();
alert("You clicked " + value);
});
});
Upvotes: 6
Reputation: 1704
Just another hacky way to get checked value with jQuery selectors:
$(".checked input[name=name-of-original-input]").val();
But AFAIK default class for checked values could be changed in configuration.
BTW, I think that iCheck isn't a good choice if it creates such issues with very simple functionality.
Upvotes: 14
Reputation: 827
I found the other answers to be useful. However, they are dependent on iCheck's 'ifClicked'. You may not want the value to pop up every time a new radio button is selected, but instead only when a user hits a specific button calling for the alert and corresponding value.
For example:
$(document).on('click', '.buttonClass', function(event) {
return alert($('.radioClass:checked').val());
});
Upvotes: 7
Reputation: 32581
You must use iCheck like this
$(document).ready(function () {
$('input[name="iCheck"]').on('ifClicked', function (event) {
alert("You clicked " + this.value);
});
$('input').iCheck({
radioClass: 'iradio_flat-orange'
});
});
Upvotes: 34