Hướng dẫn onclick trong html w3schools

Example

Execute a JavaScript when a button is clicked:

Click me

Try it Yourself »

Definition and Usage

The title event occurs when the user clicks on an element.

In JavaScript, using the addEventListener[] method:

object.addEventListener["click", myScript];

Try it Yourself »

Technical Details

Bubbles:Cancelable:Event type:Supported
HTML tags:
Yes
Yes
MouseEvent
All exept: , ,
, , , , , , , , and

More Examples

Click a to display the date:

What is the time?

Try it Yourself »

Click a

element to change the text color:

Click me to change my color.


function myFunction[] {
  document.getElementById["demo"].style.color = "red";
}

Try it Yourself »

Another example on how to change the color of an element:

Click me to change my color.


function myFunction[element, color] {
  element.style.color = color;
}

Try it Yourself »

Click to copy text from one input field to another:

Copy Text


function myFunction[] {
  document.getElementById["field2"].value =   document.getElementById["field1"].value;
}

Try it Yourself »

How to assign an " title" event to the window object:

window. title = myFunction;

  function myFunction[] {
document.getElementsByTagName["BODY"][0].style.backgroundColor = "yellow";
}

Try it Yourself »

Use title to create a dropdown:

document.getElementById["myBtn"]. title = function[] {myFunction[]};

function myFunction[] {
  document.getElementById["myDropdown"].classList.toggle["show"];
}

Try it Yourself »

Related Pages

JavaScript Tutorial: JavaScript Events

HTML DOM reference: ondblclick event

HTML DOM reference: onmousedown event

HTML DOM reference: onmouseup event

Browser Support

title is a DOM Level 2 [2001] feature.

It is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes 9-11 Yes Yes Yes Yes

❮ HTML Event Attributes

Example

Execute a JavaScript when a button is clicked:

Click me

Try it Yourself »

More "Try it Yourself" examples below.

Definition and Usage

The title attribute fires on a mouse click on the element.

Browser Support

Event Attribute
title Yes Yes Yes Yes Yes

Syntax

Attribute Values

ValueDescription
script The script to be run on title

Technical Details

Supported HTML tags:
All HTML elements, EXCEPT: , ,
, , , , , , , , and

More Examples

Example

Click on a

element to change its text color to red:

Click me to change my text color.


function myFunction[] {
  document.getElementById["demo"].style.color = "red";
}

Try it Yourself »

Example

Click on a button to copy some text from an input field to another input field:

Copy Text


function myFunction[] {
  document.getElementById["field2"].value = document.getElementById["field1"].value;
}

Try it Yourself »

Related Pages

HTML DOM reference: title event

❮ HTML Event Attributes


Chủ Đề