7

I am trying to select the option label (option with value "") from a select box through jQuery. I use the following selector:

$("[value='']");

This works in most browsers, however in IE7 it throws an exception. If I change it to the following (imho equivalent) selector, then it works fine:

$(":not(:not([value='']))");

I'd prefer not to use the latter, but can't think a of better equivalent of the prior.

Edit:

jQuery version: 1.3.1.
Exception:
Microsoft JScript runtime error: Exception thrown and not caught
on

if(S==null){throw"Syntax error, unrecognized expression: "+ab}

where

ab = "value='']"

Test setup:

To ensure nothing of my other code caused the problem I have reproduced the error in the following situation:

<html>
    <head>
        <script type="text/javascript" src="jquery-1.3.1.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                alert($("option[value='']").html());
            });
        </script>
    </head>
    <body>
        <select>
            <option value="">test</option>
            <option value="1">test1</option>
            <option value="2">test2</option>
        </select>
    </body>
</html>

Edit:

Link to bug report

Matthijs Wessels
  • 6,530
  • 8
  • 60
  • 103
  • tell your jquery version – vinczemarton Jan 23 '12 at 15:07
  • 1
    "it throws an exception" - What does it say? – Wesley Murch Jan 23 '12 at 15:08
  • @PiTheNumber actually you can turn on some lamps when the sun goes down. Just because you can't straight up change the how-to, doesn't mean you can't find a solution. – Nahydrin Jan 23 '12 at 15:13
  • 1
    try jquery 1.7.1 . It has a whole bunch of bugs like this fixed. – vinczemarton Jan 23 '12 at 15:27
  • 3
    You should upgrade to 1.3.2+ Have a look at this question: http://stackoverflow.com/questions/635240/selecting-all-empty-text-fields-in-jquery – MacMac Jan 23 '12 at 15:28
  • Sadly, upgrading is not an option for me. :( – Matthijs Wessels Jan 23 '12 at 16:02
  • 1
    @MatthijsWessels, why not? you could add jQuery 1.7.1 and use `noconflict` to have it continue to use `1.3` for whatever backwards compatibility you need, and use the newer jQuery for general-purpose code. – zzzzBov Jan 23 '12 at 16:12
  • @zzzzBov, hmm, it's thousands of lines of code we're talking about here. So I have to be 100% sure that nothing falls over. Is the following what you mean:` `? – Matthijs Wessels Jan 23 '12 at 22:26
  • @MatthijsWessels, if you're writing thousands of lines of JavaScript code for a single page, and worrying about jQuery dependencies, it sounds like you haven't organized your JS very well. – zzzzBov Jan 23 '12 at 22:40
  • @zzzzBov, please, don't get me started. It's legacy and I have to deal with it. Every change I make, I try to improve something, while not breaking anything and taking a minimal amount of time. – Matthijs Wessels Jan 24 '12 at 00:09
  • @zzzzBov, anyway thanks for pointing the noconflict thing out for me. It might come in handy. – Matthijs Wessels Jan 24 '12 at 23:40

2 Answers2

2

I would like to recommend you to use the latest jQuery version, wich would solve your problem. But if you are using this version for a good reason you should try the following:

This should work in all browsers:

alert( jQuery( 'input[value=]' ) );

Tested with JSFiddle and no errors: http://jsfiddle.net/bobkruithof/WUVHj/

halfpastfour.am
  • 5,764
  • 3
  • 44
  • 61
  • Thanks, seems that that will work, although I wonder if this will ever evaluate to "input[value]" which will return all elements with a value attribute. Btw, jsfiddle only let's me test against jQuery 1.3.2, where this bug is not present ([value=''] works fine). Nevertheless, I tested it in my test setup and your solution seems to work in 1.3.1. – Matthijs Wessels Jan 24 '12 at 10:42
  • AFAIK this will never evaluate to input[value], so you are safe :) – halfpastfour.am Jan 24 '12 at 14:25
0

Ideally it should not give you any error. Try specifying the tag type also $("input[value='']") assuming that you don't have any other elements to search with value attribute.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • "Didn't work" means you still get the same error or it does not select the right elements? What elements do you have? – PiTheNumber Jan 23 '12 at 15:50
  • Still throws the same error. I have reproduced it in a test setting with minimal elements. I will update the question with my test setting. – Matthijs Wessels Jan 23 '12 at 15:57