Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "[value='']" throw an exception in IE7 and ":not(:not([value='']))" does not?

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

like image 317
Matthijs Wessels Avatar asked Jan 23 '12 15:01

Matthijs Wessels


1 Answers

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/

like image 166
halfpastfour.am Avatar answered Oct 13 '22 00:10

halfpastfour.am