All TalkersCode Topics

Follow TalkersCode On Social Media

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

jQuery Set Radio Button Checked By ID

Last Updated : Mar 11, 2024

jQuery Set Radio Button Checked By ID

In this article we will show you the solution of jQuery set radio button checked by id, here we needs to use attr(), click() and is() 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() method used, which is purposely used for set or get elements attribute values and is() method used for checks radio button checked state.

Step By Step Guide On jQuery Set Radio Button Checked By ID :-

Here we defined html block with h3 tag for displays heading and button ‘Click Here’, two radio type input elements ‘Rose,Red’ with id’s ‘rbtn1,rbtn2’.

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 checks whether first radio button in checked state or not then we sets checked state to ‘checked’ by using attr() method.

<!DOCTYPE html>
<html>
    <head>
        <title>Check Radio Button By ID</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
            $(document).ready(function (){
                $('#rbtn2').attr("checked","checked");
                $('button').click(function(){
                    if(!($('#rbtn1').is("checked"))){
                        $('#rbtn1').attr("checked","checked");
                    }
                });
            });
        </script>
        </head>
        <body>
            <h3> Check Radio Button By ID</h3>
                <button>Click Here</button><br><br><br>
                <input type="radio" id="rbtn1">Rose
                <input type="radio" id="rbtn2">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 id rbtn2 of radio button for sets it checked state value to ‘checked’ for initially displays in checked state.
  8. Then we defined button with click() method for generate click event when user clicks on ‘Click Here’ button and within that using if condition we checks whether ‘rbtn1’ of radio button ‘Rose’ is not checked or not then we sets id ‘rbtn1’ checked state value to ‘checked’ by using attr() method which is appended with radio button’s id ‘rbtn1’.
  9. In html block we defined h3 tag for showing heading and button, two input element with radio button type ‘Rose,Red’ and id’s ‘rbtn1,rbtn2’.
  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 set radio button to checked state by using id in 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 By ID’, button ‘Click Here’ and radio buttons ‘Rose, Red’.

There checkbox Red only initially in checked state and now user needs to click on ‘Click Here’ button then radio button ‘Rose’ goes to checked state because here we specifically defined id ‘rbtn1’ with attr() method.

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