Why jQuery selector works on Chrome but not on Safari?

Viewed 646

I have the following code:

var l = $("input[name*=hello").length;
$('#result').text(l);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input name="hello" type="text">
</div>
<div>
  <input name="hello" type="text">
</div>
<div id="result">
  
</div>

Where the jQuery selector is:

input[name*=hello

Check that the selector is not correctly written input[name*=hello since a ] is missing.

On Chrome I get on the div[id=result] the amount of inputs that I have with the name "hello" (2).

But on Safari I get an error on the Console:

enter image description here

Which is correct, so the question is: Why did Chrome "autocorrect" the selector? Is it possible to remove that behaviour?

1 Answers

If the selector doesn't contain any jQuery extensions, jQuery will use the browser's built-in document.querySelectorAll() function. When the selector is invalid, the behavior is unspecified, so it may differ between browsers. Some browsers try to "fix" the selector and return something useful, while other browsers signal an error. You shouldn't depend on this -- write correct selectors.

Related