Table of contents
Do want to add radio button to your form but don't have any idea how to get the value using jquery? This post is for you I will share 3 methods on how to get the radio button selected value when clicking the button event.
Â
First Method
Getting radio button value using class
// Method #1 - Getting radio selected using class
$("#btnSubmit1").on("click", function() {
var membership = $(".membership1:checked").val();
alert(membership);
});
Â
Second Method
Getting radio button selected value using attribute name with value
// Method #2 - Getting radio selected value using attribute name & value
$("#btnSubmit2").on("click", function() {
var membership = $("[name=\"membership2\"]:checked").val();
alert(membership);
});
Â
Third Method
Getting radio selected value using attribute type & value "[name=\"radio\"]:checked"
// Method #3 - Getting radio selected value using attribute type & value "[name=\"radio\"]:checked"
$("#btnSubmit3").on("click", function() {
var membership = $("[type=\"radio\"]:checked").val();
alert(membership);
});
Take note that the third method is reading all the radio button within the page better to call the radio button element using method #1 and #2.
Â
That's it I hope this simple post help you. If you think this is helpful please share this to your friend.
Â
Happy coding :)
Read next