Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input vs :Input in jQuery

I wonder why people seems to prefer :input over input as a jQuery selector? Basically, this two lines seem to do the same thing :

$('input:first').focus()
$(':input:first').focus()

But second version is more widely use, and I don't find why. Moreover, the :input selector seem slower according to this benchmark: http://jsperf.com/input-vs-input/2

like image 332
Hartator Avatar asked Feb 13 '13 21:02

Hartator


People also ask

What is input in jQuery?

version added: 1.0jQuery( ":input" ) The :input selector basically selects all form controls.

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.


1 Answers

:input is pseudo selector by jQuery which includes <buttons>, <textarea>, e.t.c

input is a tag match which strictly matches <input>.

This additional note about :input is informative:

Because :input is a jQuery extension and not part of the CSS specification, queries using :input cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :input to select elements, first select the elements using a pure CSS selector, then use .filter(":input").

--from https://api.jquery.com/input-selector/

like image 146
Selvakumar Arumugam Avatar answered Sep 21 '22 04:09

Selvakumar Arumugam