2

I'm using bootstrap buttons, which look like regular buttons but they act like radio.

<div class="btn-group-horizontal" data-toggle="buttons" id ="Group">
  <label class="btn btn-sm btn-info btnSelect active">
    <input type="radio" name="options" id="Option 1" autocomplete="off" checked> Option 1
  </label>
  <label class="btn btn-sm btn-info btnSelect">
    <input type="radio" name="options" id="Option 2" autocomplete="off"> Option 2
  </label>
</div>

How can I get the ID / text of the active button?

Thanks!

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
Vika
  • 153
  • 1
  • 1
  • 10
  • 1
    possible duplicate of [How can I get which radio is selected via jQuery?](http://stackoverflow.com/questions/596351/how-can-i-get-which-radio-is-selected-via-jquery) – Igor Kustov Dec 07 '14 at 09:44

3 Answers3

10

The solution was the following:

I had to change the HTML by addiong ID to the lable

<div class="btn-group-horizontal" data-toggle="buttons" id ="Group">
  <label class="btn btn-sm btn-info btnSelect active" **id="Option 1"**>
    <input type="radio" name="options" id="Option 1" autocomplete="off" checked> Option 1
  </label>
  <label class="btn btn-sm btn-info btnSelect" **id="Option 2"**>
    <input type="radio" name="options" id="Option 2" autocomplete="off"> Option 2
  </label>
</div>

and by using following jQuery:

var answer= '';
            $('#Group .active').each(function(){
                answer= $(this).attr('id'); 
            }); 
Vika
  • 153
  • 1
  • 1
  • 10
6

You can just use a simple CSS selector

$(document).ready(function () {
    alert($('.active input').prop('id'));
});

Demo: http://jsfiddle.net/robschmuecker/L7maoufy/

Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
1

Can try using each() & is(). Example:

var activeId = '';
$('input[type=radio]').each(function(){
    if($(this).is(':checked')){
        activeId = $(this).attr('id'); 
        //alert(activeId);
        return false;
    }
});
MH2K9
  • 11,951
  • 7
  • 32
  • 49