Attributo evento HTML onfocus

❮ Attributi degli eventi HTML

Esempio

Esegui un JavaScript quando un campo di input diventa attivo:

<input type="text" id="fname" onfocus="myFunction(this.id)">

Altri esempi "Provalo da solo" di seguito.


Definizione e utilizzo

L'attributo onfocus si attiva nel momento in cui l'elemento viene messo a fuoco.

Onfocus viene spesso utilizzato con <input>, <select> e <a>.

Suggerimento: l'attributo onfocus è l'opposto dell'attributo onblur .


Supporto browser

Event Attribute
onfocus Yes Yes Yes Yes Yes

Sintassi

<element onfocus="script">

Valori di attributo

Value Description
script The script to be run on onfocus

Dettagli tecnici

Tag HTML supportati: Tutti gli elementi HTML, TRANNE: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> e <title>

Altri esempi

Esempio

Usando "onblur" insieme all'attributo "onfocus":

<input type="text" onfocus="focusFunction()" onblur="blurFunction()">

Pagine correlate

Riferimento HTML DOM: evento onfocus


❮ Attributi degli eventi HTML