We can get HTML attribute values with the help of JQuery attr() function. If you had an array that has name-value pairs and you wanted to use the index from the array as the value for the options and the array value as the text to display, you could do this: When present, it specifies that an option should be pre-selected when the page loads. var optionCollection = Array.from(select.options).filter(x => x.text.toLowerCase().startsWith(keyword.toLowerCase())) this will get rid of the for..loopyou had. selected Optional Selected Reading; UPSC IAS Exams Notes; Developer's Best Practices; Questions and Answers; Effective Resume Writing; HR Interview Questions; Computer Glossary ; Who is Who; How to get the value of custom attribute in jQuery? You can set the initial state with the SELECTED attribute, while the selected property contains the actual state of the option. To select the single element, we need to use document.querySelector() method by passing a [data-attribute = 'value'] as an argument. This is the equivalent of jQuery's $.data() method. An option is considered selected if it has an HTMLOptionElement.selected attribute. The SELECTED attribute in HTML and the selected property in JavaScript work differently. The JavaScript function above takes in two parameters. If no option selected … In vanilla JavaScript setting a data attribute of an element is done with the generic setAttribute() method. javascript by Inexpensive Impala on Jul 22 2020 Donate . Grouping options in a select control may improve readability and ease of use. We have also demostrate about the jquery set data attribute below. The difference between defaultSelected and selected is that defaultSelected sets the HTML-attribute (that we can get using option.getAttribute('selected'), while selected sets whether the option is selected … You can include a selected attribute on an