3

I'd like to get the value of the checked checkbox that has the radioListMode class:

<label class="btn btn-secondary active">
    <input type="radio" class="radioListMode" value="cards" checked>Cards
</label>
<label class="btn btn-secondary">
    <input type="radio" class="radioListMode" value="pins">Pins
</label>

From this previous question, this is what I have tried and it is undefined:

console.log($('input[class="radioListMode"]:checked').value);

I do not wish to add a name because the value of this checkbox is only used for layout selection, not any data input.

Community
  • 1
  • 1
davidtgq
  • 3,780
  • 10
  • 43
  • 80
  • Have you tried $('input.class="radioListMode"]:checked').val() – progrAmmar Feb 23 '16 at 02:27
  • I don't understand why you wouldn't add an ID. The extra amount of data sent would not make much of a difference in bandwidth costs unless you were processing billions (with a b) of users a month. – Jon Feb 23 '16 at 02:27

4 Answers4

4

Since you seem to be using jQuery, stick with it:

$('input.radioListMode:checked').val();
dave
  • 62,300
  • 5
  • 72
  • 93
  • Thanks. I didn't know that `input.radioListMode` would work, is that just a shortcut for the square bracket notation with classes? – davidtgq Feb 23 '16 at 03:15
  • 1
    Yes they do the same thing. The square bracket notation is not used very often in my experience. Much more common to see .className and #id – dave Feb 23 '16 at 04:18
2

Use .val() property

$('input[class="radioListMode"]:checked').val()

The Process
  • 5,913
  • 3
  • 30
  • 41
1

.value is not a jQuery function, instead use .val() like following :

console.log($('input[class="radioListMode"]:checked').val());
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40
0
$(".yourClassName:checkbox:checked").each(function() {
     console.log($(this).attr("id"));
});
Sharhabeel Hamdan
  • 1,273
  • 13
  • 15