Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iCheck library: value of selected radio button

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);
  });
 });
like image 271
Clive van Hilten Avatar asked Sep 10 '13 13:09

Clive van Hilten


4 Answers

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'
    });
});

DEMO

Documentation

like image 102
Anton Avatar answered Sep 24 '22 18:09

Anton


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.

like image 22
simplylizz Avatar answered Sep 21 '22 18:09

simplylizz


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());
});
like image 7
Sean L Avatar answered Sep 24 '22 18:09

Sean L


Try this out:- http://jsfiddle.net/adiioo7/yhmu97qh/

JS:-

$(document).ready(function () {
    $('input').iCheck({
        radioClass: 'iradio_flat-orange'
    });

    $('input').on('ifClicked', function (event) {
        var value = $(this).val();
        alert("You clicked " + value);
    });
});
like image 6
Aditya Singh Avatar answered Sep 20 '22 18:09

Aditya Singh