All TalkersCode Topics

Follow TalkersCode On Social Media

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

jQuery Check Radio Button

Last Updated : Mar 11, 2024

jQuery Check Radio Button

In this article we will show you the solution of jQuery check radio button, here we needs to use attr(), click() and prop() methods for achieve the result.

The click() method triggers the click event, or attaches a function to run when a click event occurs then attr() and prop() methods used they almost have same behavior, which is purposely used for set or get elements attribute values in jquery.

Step By Step Guide On jQuery Check Radio Button :-

Here we defined html block with h3 tag for displays heading and button ‘Click Here’, radio type input element ‘Red’ with class ‘btn’.

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 sets initial state of input ‘Red’ in unchecked state then we appended click method on button there we changing checked state to ‘true’ for make it as checked state by using prop() method.

<!DOCTYPE html>
<html>
    <head>
        <title>Check Radio Button</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
            $(document).ready(function (){
                $('.btn').attr("checked",false);
                $('button').click(function(){
                    $('.btn').prop("checked",true);
                });
            });
        </script>
        </head>
        <body>
            <h3>Check Radio Button</h3>
                <button>Click Here</button><br><br><br>
                <input type="radio" class="btn">Red
        </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 attr() method with class btn of radio button for sets it checked state value to ‘false’ for initially displays in unchecked state and as we know it’s default state also unchecked.
  8. Then we defined button with click() method for generate click event when user clicks on ‘Click Here’ button and then we sets radio button ‘Red’ class ‘btn’ checked state value to ‘true’ by using prop() method which is appended with radio button’s class ‘btn’.
  9. In html block we defined h3 tag for showing heading and button, input element with radio button type ‘Red’ and class ‘btn’.
  10. 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 how to check radio button using jquery.

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 heading ‘Check Radio Button’, button ‘Click Here’ and radio button ‘Red’ in unchecked state now user needs to click on ‘Click Here’ button then radio button goes to checked state.

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

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪