All TalkersCode Topics

Follow TalkersCode On Social Media

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

jQuery Select Option By Value

Last Updated : Mar 11, 2024

jQuery Select Option By Value

In this article we will show you the solution of jQuery select option by value, here we needs to define select element with some options and use find() and attr() methods for achieve the results.

The 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 jQuery Select Option By Value :-

In html we defined select element with id attribute and five options, button ‘Select’.

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 defined click() method which is triggers by clickable element on webpage and within this we specified select element by id.

Which is appends with find method, it is used for particularly specifies option with value and that is appends with attr() for sets that option’s selected attribute value set to ‘selected’ state.

<!DOCTYPE html>
<html>
    <head>
        <title>Select option by value</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("#sub").click(function(){
                $('#sel').find('option[value="4"]').attr("selected","selected");
                })
            });
        </script>
    <body>
        <input type="submit" value="Select" id="sub">
        <select id="sel">
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
            <option value="4">Four</option>
            <option value="5">Five</option>
        </select>
    </body>
    </head>
</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. We defined select button with id ‘sub’, select element with id ‘sel’ and options ‘One,Two,Three,Four,Five’ with value attribute for sets respected values.
  8. In script we defined ready() method within that we appended click() method on button ‘Select’ by using id ‘sub’.
  9. There we referring select element by id ‘sel’ then which is appends with find and attr methods. In find() method option-refers option’s of select element and within square bracket ‘value="4"’ refers Four option.
  10. In attr() method we sets ‘selected’ attributes value to ‘selected’ for select that option 4 from select element.
  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 how to use each method with json array 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 Select button and dropdown element with defined options.

Now user needs to click on select button then dropdown default first option value changed to our expected ‘Four’ option.

I hope this article on jQuery select option by value helps you and the steps and method mentioned above are easy to follow and implement.