Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:06:30+00:00 2026-06-13T23:06:30+00:00

Scenario: I got 2 jQuery expressions: /* A */ $(‘select’).find(‘option[selected]’); /* B */ $(‘select’).find(‘option’).filter(‘[selected]’);

  • 0

Scenario:

I got 2 jQuery expressions:

/* A */ $('select').find('option[selected]');
/* B */ $('select').find('option').filter('[selected]');

which mean (let’s assume there’s only one select in the document, for simplicity):

  • A: Get the select, then find all option descendants that has an attribute named selected.
  • B: Get the select, then find all option descendants, then filter by those who has an attribute named selected.

Expected Behaviour:

A and B should give the same result.

Actual Behaviour:

After the user changed the selection in the dropdown,

  • A returns the default selected option.
  • B returns the new selected option.

Question:

So why are they different? Is my understanding about CSS selectors wrong?

Live Demo:

Live demo is here here.

Source Code:

HTML:

<select>
 <option value='p'>p</option> 
 <option value='q' selected>q</option>
 <option value='r'>r</option> 
 <option value='s'>s</option> 
</select>


<input type='button' value='click me!'/> <br/> 
 ResultA : <span id='ResultA'>
    here
</span> <br/> 
 ResultB : <span id='ResultB'>
    here
</span> <br/> 

Javascript:

function SetResult(ResultObj, ElementObj) {
    ResultObj.text("length=" + ElementObj.length + " " + "val()=" + ElementObj.val());
}

$(function() {
    $('input[type=button]').click(function() {
        var SelectObj = $('select');
        SetResult($("#ResultA"), SelectObj.find('option[selected]'));
        SetResult($("#ResultB"), SelectObj.find('option').filter('[selected]'));
    });
});

Test Result:

+---------------------------+--------------+---------------------+---------+-----+
|          Browser          | Environment  |       jQuery        |    A    |  B  |
+---------------------------+--------------+---------------------+---------+-----+
| Chrome 22.0.1229.94m      | Win7         | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Chrome 23.0.1271.64 m     | Win7         | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 15.0.1            | Win7         | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 16.0.2            | Win7         | 1.8.2, 1.7.2, 1.6.4 | default | new |
| IE 6                      | WinXP        | 1.8.2, 1.7.2, 1.6.4 | *new*   | new |
| IE 9                      | Win7         | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 12.02               | Win7         | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 12.10               | Win7         | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Safari 5.1.7 (7534.57.2)  | Win7         | 1.8.2, 1.7.2, 1.6.4 | default | new |
+---------------------------+--------------+---------------------+---------+-----+
| Chrome 22.0.1229.94       | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Chrome 23.0.1271.64       | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 13.0              | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 14.0.1            | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Firefox 16.0.2            | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 12.01               | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 12.10               | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Safari 6.0.1 (7536.26.14) | MacOS 10.7.5 | 1.8.2, 1.7.2, 1.6.4 | default | new |
+---------------------------+--------------+---------------------+---------+-----+
| Chrome 21.0.1180.82       | iOS 4.3.5    | 1.8.2, 1.7.2, 1.6.4 | default | new |
| Opera 7.0.5               | iOS 4.3.5    | 1.8.2               | default | new |
| Safari                    | iOS 4.3.5    | 1.8.2, 1.7.2, 1.6.4 | default | new |
+---------------------------+--------------+---------------------+---------+-----+
  • default means it returns the default selected option.
  • new means it returns the new selected option.

As you can see, all browsers except IE6 give different results.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:06:31+00:00Added an answer on June 13, 2026 at 11:06 pm

    What should the expressions return?

    [selected] matches all elements that has a selected attribute, with any value (references: W3C, jQuery).


    Why are the results inconsistent?

    I have submitted a bug report to jQuery here. It is marked as a duplicate of another bug report which is now fixed.


    Any solutions?

    To get the current selection:

    Use the :selected selector (live demo here):

    $('select').find('option').filter(':selected'); /* Supposedly faster */
    or
    $('select').find('option:selected');            /* Supposedly slower */
    

    Note that the 2nd expression is supposedly slower, according to the doc.

    Because :selected is a jQuery extension and not part of the CSS specification, queries using :selected cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method.

    To get the default selection:

    For jQuery 1.9+, use any of the expressions as in the question, i.e.

    /* A */ $('select').find('option[selected]');
    /* B */ $('select').find('option').filter('[selected]');
    

    For jQuery 1.6+, make use of the defaultSelected property (live demo here, references: w3schools, Mozilla, MSDN, MSDN):

    $('select').find('option').filter(function() {
        return $(this).prop('defaultSelected');
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got scenario where i would like to find where text in UILabel
I've got a button for which I dynamically create target URI with jquery. Controller
This is a simple question, I came across a scenario which got me thinking
I've got a scenario where I have Image Sources set to null which are
I got this Jquery code : $.ajax({ url: ?module=gestionApplication&action=getTests&scenario=+encodeURI(scenario)+&application=+$application, dataType:'json', success: function( data )
Here's the thing.. Scenario: I got a ipa file which I get as a
I got a scenario like this Class Parent { Property A; } Class A
Scenario I've got a windows service written in C#. I've read all the google
I've got a scenario where I want to switch on two different tables in
I've got the scenario with two NSManagedObjects, Arm and Person. Between them is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.