-1

how can i get the value of radiobox in jquery

<input type="radio" name="religion" class="chk" name="religion" value="muslim">Muslim
<input type="radio" name="religion" class="chk" name="religion" value="christian">Christian
<input type="radio" name="religion" class="chk" name="religion" value="hindu">Hindu

i have tried this, but i am not getting the value, how can i do this ?

var religion = $("name=religion").val();
alert(religion)
priya
  • 11
  • 2

4 Answers4

1

You should get value of pressed checkbox by

var religion = $("input[name=religion]:checked").val();
alert(religion)
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
0

const radio1Value = document.querySelector("input").value;
console.log(radio1Value);
<input type="radio" name="religion" class="chk" name="religion" value="muslim">Muslim
<input type="radio" name="religion" class="chk" name="religion" value="christian">Christian
<input type="radio" name="religion" class="chk" name="religion" value="hindu">Hindu
Felix Fong
  • 969
  • 1
  • 8
  • 21
0

try this

$(document).ready(function(){
        $("input[type='button']").click(function(){
            var radioValue = $("input[name='religion']:checked").val();
            if(radioValue){
                alert("Your are a - " + radioValue);
            }
        });

    });

Demo

santosh singh
  • 27,666
  • 26
  • 83
  • 129
-1

You have name="religion" twice. You need to change one of them to id="religion". then you can access the value with

var religion = $("#religion").val();
SunKnight0
  • 3,331
  • 1
  • 10
  • 8