All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Set Selected Value Of Dropdown In jQuery

Last Updated : Mar 11, 2024

Set Selected Value Of Dropdown In jQuery

In this article we will show you the solution of set selected value of dropdown in jQuery, here we needs to use ready(), find() and attr() methods for achieve the results. As we know find() method returns descendant elements of the selected element.

A descendant is a child, grandchild, great-grandchild and so on. The attr() method sets or returns attributes and values of the selected elements. It returns the value of the FIRST matched element.

Step By Step Guide On Set Selected Value Of Dropdown In jQuery :-

In html we defined select element with three options. In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser.

There we specified select element which is appends with find method, it is used for set default option value as selected option and it is appends with attr() for sets that option’s selected attribute value set to ‘selected’ state.

<!DOCTYPE html>
        <title>Set selected value in dropdown</title>
        <script src=""></script>
            <option value="ap">Apache</option>
            <option value="re">Royal Enfield</option>
            <option value="zr">ZR</option>
  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. We defined select element with options ‘Apache, Royal Enfield, ZR’ with value attribute for sets respected values ‘ap,re,zr’.
  8. In script we defined ready() method within that we referring select element by tag name ‘select’ then which is appends with find and attr methods. In find() method option-refers option’s of select element and within square bracket ‘value="zr"’ refers third option ‘ZR’.
  9. In attr() method we sets ‘selected’ attributes value to ‘selected’ for select that particular option from select element.
  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 selected value on dropdown 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 dropdown element with default option ZR because we sets that option as default in case you need to change just modify at find() method option value.

I hope this article on set selected value of dropdown in jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪