In JavaScript:
object.onfocus = function(){
myScript};
This example uses the HTML DOM to assign an "onfocus" event to an input element.
<!DOCTYPE html> <html> <body> Enter your name: <input type="text" id="fname"> <script> document.getElementById("fname").onfocus = function() {myFunction()}; function myFunction() {/*w w w.j a va 2s .c o m*/ document.getElementById("fname").style.backgroundColor = "red"; } </script> </body> </html>
Bubbles: Cancelable: Event type: Supported HTML tags: | No No FocusEvent ALL HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
---|