jQuery Selected Option Selector


The Selected Option Selector in jQuery is used to target <option> elements within a <select> element that are currently selected by the user. This selector allows you to manipulate or retrieve information about the options that are actively chosen in dropdown menus.

Syntax

$("select option:selected")

How It Works

  • The :selected pseudo-class selects <option> elements that are currently selected within a <select> element.
  • It can be used to get the value or text of the selected options, or to apply styles or perform actions based on the selected options.

Example Usage

1. Getting the Selected Option's Value

You can use the Selected Option Selector to retrieve the value of the currently selected option in a dropdown menu.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Selected Option Selector Example</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { $("#getSelectedValue").on("click", function() { var selectedValue = $("select option:selected").val(); alert("Selected value: " + selectedValue); }); }); </script> </head> <body> <form> <select id="dropdown"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select> <button type="button" id="getSelectedValue">Get Selected Value</button> </form> </body> </html>

Explanation:

  • $("select option:selected") selects the currently selected <option> within the <select> element. Clicking the button with ID getSelectedValue triggers an alert showing the value of the selected option.

2. Getting the Selected Option's Text

You can also get the text content of the selected option.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Selected Option Selector Example</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { $("#getSelectedText").on("click", function() { var selectedText = $("select option:selected").text(); alert("Selected text: " + selectedText); }); }); </script> </head> <body> <form> <select id="dropdown"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select> <button type="button" id="getSelectedText">Get Selected Text</button> </form> </body> </html>

Explanation:

  • $("select option:selected") selects the currently selected <option> and .text() retrieves the text content of that option.

3. Applying Styles to the Selected Option

You can use the Selected Option Selector to apply specific styles to the selected option.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Selected Option Selector Example</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { $("#highlightSelected").on("click", function() { $("select option:selected").css("background-color", "yellow"); }); }); </script> </head> <body> <form> <select id="dropdown"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select> <button type="button" id="highlightSelected">Highlight Selected Option</button> </form> </body> </html>

Explanation:

  • $("select option:selected") selects the currently selected option and .css("background-color", "yellow") applies a yellow background color to it.

Use Cases

  • Form Validation: Retrieve selected values or texts to validate user inputs before form submission.
  • Dynamic Updates: Apply styles or update the UI based on the selected option in a dropdown.
  • User Interaction: Provide feedback or perform actions based on the option the user selects.

Performance Considerations

  • The Selected Option Selector is efficient for selecting and interacting with dropdown options. However, ensure that it’s used within the context of a <select> element to avoid unintended selections.