M Argus Chopin Gyver
M Argus Chopin Gyver

Reputation: 304

Get radio button value by the group

I have a bunch of radio button on my single page, and I want to get a value from the checked one

Here's my HTML Code

<div class="row">
//first radio button group
    <div class="col-md-1 col-xs-1 col-sm-1">&nbsp;</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="1"><br>1<!--<br>Awful--></div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="2"><br>2</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="3"><br>3</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="4"><br>4</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="5"><br>5<!--<br>Ok--></div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="6"><br>6</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="7"><br>7</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="8"><br>8</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="9"><br>9</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="10"><br>10<!--<br>Incridible--></div>
    <div class="col-md-1 col-xs-1 col-sm-1">&nbsp;</div>
</div>

I've tried using loop like this

var pertanyaan1 = 1;
        var isipertanyaan1 = 0;
        $("input[name=pertanyaan1]").each(function(){

            if($("input[name=pertanyaan1][value="+pertanyaan1+"]").prop("checked","checked"))
            {
                console.log($("input[name=pertanyaan1][value="+pertanyaan1+"]").prop("checked","checked"));
                isipertanyaan1 = pertanyaan1;
                // break();
            }
            // console.log(pertanyaan1);
            pertanyaan1++;
        });

but that code make the if useless.. The radio not filtered..

how to solve this ?

Upvotes: 1

Views: 3512

Answers (2)

Syed Mhamudul Hasan
Syed Mhamudul Hasan

Reputation: 1349

This code may help you

<script>
        $(function () {

            $('input[type=radio]').on('click', function () {

                var value = $(this).attr('value');
                console.log(value);
                if ($(this).is(':checked')) {
                   // alert("it's checked");
                    console.log("it's checked");
                }
            });
        });
    </script>

Upvotes: 0

Mohit Tanwani
Mohit Tanwani

Reputation: 6628

No need to use loop to get selected Radio button value.

just use :checked

$("input[name=pertanyaan1]:checked").val();

$('button').click(function(){
  console.log($("input[name=pertanyaan1]:checked").val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
//first radio button group
    <div class="col-md-1 col-xs-1 col-sm-1">&nbsp;</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="1"><br>1<!--<br>Awful--></div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="2"><br>2</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="3"><br>3</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="4"><br>4</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="5"><br>5<!--<br>Ok--></div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="6"><br>6</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="7"><br>7</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="8"><br>8</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="9"><br>9</div>
    <div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="10"><br>10<!--<br>Incridible--></div>
    <div class="col-md-1 col-xs-1 col-sm-1">&nbsp;</div>
</div>

<button>Get Selected Value</button>

Upvotes: 5

Related Questions