How to use onclick event in JavaScript
onclick is an event, which will be processed when the event is triggered. onclick is an event that handles mouse clicks. This article will share with you the usage of onclick events in How to use onclick event in How to use onclick event in How to use onclick event in JavaScript.
Let’s first take a look at the syntax of the onclick event
The following is how to use the onclick event to write.
Use document.getElementById() to specify the id element in the document, and use function(){} to handle the event that occurs when the element is clicked.
document.getElementById("button").onclick = function() { // 设置在此处单击#button时要发生的事件 };
Let’s look at specific examples
The following is an example of using the onclick event.
Change text when button is clicked
HTML code
<div id="text-button"><p id="text">点击</p></div>
CSS code
#text-button { display: block; cursor: pointer; width: 160px; text-align: center; border: 1px solid #232323; }
How to use onclick event in How to use onclick event in How to use onclick event in JavaScript code
document.getElementById("text-button").onclick = function() { document.getElementById("text").innerHTML = "我点击了!"; };
The result displayed on the browser is as follows
When this box is clicked, the following effect will be displayed: the text in the box has changed
When the box is clicked, the background color of the box changes
HTML code
<div id="square-button"></div>
CSS code
#square-button { width: 80px; height: 80px; background: #232323; } #square-button.blue { background: #21759b; }
How to use onclick event in How to use onclick event in How to use onclick event in JavaScript Code
document.getElementById("square-button").onclick = function() { this.classList.toggle("blue"); };
The browser displays the following effect: It is a black box
When this box is clicked, the color will change and the display effect As follows
Display the content entered in the form
HTML code
<p>你叫什么名字?</p> <input type="text" id="name"> <button type="button" id="form-button">输入</button> <div id="form-text"></div>
CSS Code
:focus { outline: 1px solid #666; } input[type="text"] { margin: 0 0 15px; padding: 8px 10px; border: 1px solid #d0d1d3; } button { padding: 8px 15px; background: #979380; color: #fff; border: none; }
How to use onclick event in How to use onclick event in How to use onclick event in JavaScript code
document.getElementById("form-button").onclick = function() { document.getElementById("form-text").innerHTML = "你好 " + document.getElementById("name").value + " 同学!"; }
The display effect on the browser is as follows
When you enter a name in the text box, such as Zhang 3. Then click Enter and the following effect will be displayed
The above is the detailed content of How to use onclick event in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
