All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Radio Button Checked jQuery

Last Updated : Mar 11, 2024

Radio Button Checked jQuery

In this article we will show you the solution of radio button checked jQuery, here we needs to use click() and val() methods for displays clicked radio button value as result. The click() method triggers the click event, or attaches a function to run when a click event occurs.

The val() method returns or sets the value attribute of the selected elements and which is returns the value of the value attribute of first matched element.

Step By Step Guide On Radio Button Checked jQuery :-

Here we defined html block with h3 tag for displays heading and two input elements ‘Yes, No’ with id’s ‘cal1, cal2’.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we appended click method on both radio buttons by id’s ‘cal1, cal2’ for displays clicked radio button value as result by val() method.

<!DOCTYPE html>
<html>
    <head>
        <title>Checked Radio Button</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
            $(document).ready(function (){
                $('#cal1').click(function(){
                        alert($('#cal1').val()+" is Clicked");
                    });
                    $('#cal2').click(function(){
                        alert($('#cal2').val()+" is Clicked");
                    });
                });
        </script>
        </head>
        <body>
            <h3>Select Anyone Option</h3>
                <input type="radio" id="cal1" value="Yes">Yes
                <input type="radio" id="cal2" value="No">No
        </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we appending click method with checkbox ‘cal1,cal2’ which helps us for load some process when click event triggered.
  8. When user clicks on anyone defined radio button then respective click event will loads respective alert method.
  9. There we collecting clicked radio button value by val() then displayed on alertbox with string ‘is Clicked’.
  10. In html block we defined h3 tag for showing heading and two input element with radio button type ‘Yes, No’ and id’s cal1, cal2.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know radio button checked state using jquery click method.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see ‘Yes, No’ of radio buttons with heading ‘Select Anyone Option’ now user needs to click on anyone button then clicked button value displayed on alert box like ‘Yes is Clicked’.

I hope this article on radio button checked jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪