Showing posts with label Getting radio button value using JQuery. Show all posts
Showing posts with label Getting radio button value using JQuery. Show all posts

Wednesday, July 15, 2015

Getting radio button value using JQuery

This is how my HTML looks and I am going to show to get value of  the radio button that is selected :



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery-2.1.4.js"></script>
     <script type="text/javascript">
         $(document).ready(function () {
           
        $('#btnSubmit').click(function () {

                 var result = $('input[type="radio"]:checked')

                 if (result.length > 0) {
                     $('#divResult').html(result.val() + " is checked");
                 }

                 else {
                     $('#divResult').html(" Nothing is checked");
                 }

             });
         });
       

</script>
 
</head>
<body>
   Gender :
    <input type="radio" name="gender" value="Male" />Male
    <input type="radio" name="gender" value="Female" />Female
        <br />
    <br />
<input id="btnSubmit" type="submit" value="submit" />
    <br />
    <br />
    <div id="divResult">

    </div>
</body>
 

</html>



Results : 


If nothing is selected and if clicked on Submit button ,  it displays message : "Nothing is checked"




If any radio button is selected and if clicked on Submit button, it displays value of respective checkbox