thecodeparadox
thecodeparadox

Reputation: 87073

jQuery: .select() and .focus() method difference

In jQuery, what is basic difference between .select() & focus() and what are their appropriate using places?

Upvotes: 20

Views: 15910

Answers (2)

Aleksi Yrttiaho
Aleksi Yrttiaho

Reputation: 8446

From the jQuery documentation:

select

The select event is sent to an element when the user makes a text selection inside it. This event is limited to <input type="text">fields and <textarea> boxes.

focus

The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<input>, <select>, etc.) and links (<a href>). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property.

Upvotes: 7

Gary Green
Gary Green

Reputation: 22395

They have their differences:

  • .select(): will fire when TEXT is selected.
    • Limitted to <input> and <textarea> elements.
  • .focus(): will fire when an element receives focus i.e. an input box is clicked on, tabbed into, etc.
    • Also limitted but to a wider range of elements, mostly form elements such as <input>, <select>, <a>

Upvotes: 14

Related Questions