Search results
The onclick event occurs when the user clicks on an HTML element. onclick is a DOM Level 2 (2001) feature. It is fully supported in all browsers: Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
- Try It Yourself
Try out JavaScript code snippets in the W3Schools Tryit...
- Onclick
The onclick attribute fires on a mouse click on the element....
- JS Events
In the following example, an onclick attribute (with code),...
- Try It Yourself
The onclick attribute fires on a mouse click on the element. HTML DOM reference: onclick event. Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
In the following example, an onclick attribute (with code), is added to a <button> element: In the example above, the JavaScript code changes the content of the element with id="demo". In the next example, the code changes the content of its own element (using this.innerHTML): JavaScript code is often several lines long.
10 paź 2012 · You can also use the non-inline alternative, which attached to and executed from the specific element context which allows you to access the element from this. <script type="text/javascript"> document.getElementById('bar').onclick = function() { this.innerHTML = "Not a button"; }; </script> <button id="bar">Button</button>
Click on a <button> element to display the current day, date and time: <button onclick="getElementById('demo').innerHTML = Date()">What is the time?</button> Try it Yourself »
The onclick attribute fires on a mouse click on the element. The onclick attribute is part of the Event Attributes, and can be used on any HTML elements.
A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element. To execute code when a user clicks on an element, add JavaScript code to an HTML event attribute: onclick= JavaScript