Button Adding an event listener to a button click can be done using JavaScript. The code below will add a listener to the button with the id myButton , and run the ...
HTML Button onClick Buttons in their simplest form contain uppercase text, a slight elevation, hover effect, and a ripple effect on click. Default Outlined Tonal Text Plain.
How to trigger a button click from script To do this, we register the listener on the parent container and allow all clicks to bubble up to that element. We then poll the event target ...
Buttons · Bootstrap Метод OnClick также позволяет производным классам обрабатывать событие, не присоединяя делегат. Это предпочтительная методика обработки событий в производном ...
Add buttons to your app | Views When I run the script below it will trigger the function, but the click event will not be added to the button. So I cannot use the button to ...
Button behavior and examples - Streamlit Docs count++. } </script>. <!-- the function will be called on click -->. <button on:click={incrementCount}>. Clicked {count} {count === 1 ? 'time': 'times'}. </ ...
When Is A Button Not A Button? Let's say you have a part of an interface that the user clicks and something happens. Sounds like a button to me, but let's call it a “clicky ...
Tutorials Exercises Certificates Services Menu Search field × Log in Sign Up ★ +1 My W3Schools Get Certified Spaces For Teachers Plus Get Certified Spaces For Teachers Plus My W3Schools Tutorials Exercises Certificates Services Spaces Get Certified Plus Academy Logout ×
Learn AI Tutorial Learn Generative AI Tutorial Learn ChatGPT-3.5 Tutorial Learn ChatGPT-4 Tutorial Learn Google Bard Tutorial Learn Machine Learning Tutorial Learn DSA Tutorial Learn Data Science Tutorial Learn NumPy Tutorial Learn Pandas Tutorial Learn SciPy Tutorial Learn Matplotlib Tutorial Learn Statistics Tutorial Learn Excel Tutorial Learn Google Sheets Tutorial
Web Building
Create a Website HOT! Create a Server NEW Where To Start Web Templates Web Statistics Web Certificates Web Development Code Editor Test Your Typing Speed Play a Code Game Cyber Security Accessibility Join our Newsletter
Create a Website HOT! Create a Server NEW Where To Start Web Templates Web Statistics Web Certificates Web Development Code Editor Test Your Typing Speed Play a Code Game Cyber Security Accessibility Join our Newsletter
Backend
Learn Python Tutorial Reference Learn SQL Tutorial Reference Learn MySQL Tutorial Reference Learn PHP Tutorial Reference Learn Java Tutorial Reference Learn C Tutorial Reference Learn C++ Tutorial Reference Learn C# Tutorial Learn R Tutorial Learn Kotlin Tutorial Learn Go Tutorial Learn Django Tutorial Reference Learn PostgreSQL Tutorial Learn TypeScript Tutorial Learn ASP Tutorial Reference Learn Node.js Tutorial Reference Learn Raspberry Pi Tutorial Learn Git Tutorial Learn MongoDB Tutorial Learn AWS Cloud Tutorial Learn XML Tutorial Reference
Data Analytics
Learn AI Tutorial Learn Generative AI Tutorial Learn ChatGPT-3.5 Tutorial Learn ChatGPT-4 Tutorial Learn Google Bard Tutorial Learn Machine Learning Tutorial Learn DSA Tutorial Learn Data Science Tutorial Learn NumPy Tutorial Learn Pandas Tutorial Learn SciPy Tutorial Learn Matplotlib Tutorial Learn Statistics Tutorial Learn Excel Tutorial Learn Google Sheets Tutorial
Web Building
Create a Website HOT! Create a Server NEW Where To Start Web Templates Web Statistics Web Certificates Web Development Code Editor Test Your Typing Speed Play a Code Game Cyber Security Accessibility Join our Newsletter ×
Full Access Best Value! Front End Certificate Course Web Dev. Certificate Course Web App Certificate Course Web Design Certificate Course
Programs
Full Access Best Value! Front End Certificate Course Web Dev. Certificate Course Web App Certificate Course Web Design Certificate Course
Backend
Python Certificate Course SQL Certificate Course MySQL Certificate PHP Certificate Course Java Certificate Course C Certificate C++ Certificate Course C# Certificate Course R Course Django Certificate TypeScript Certificate Course XML Certificate Course Cyber Security Certificate Course Accessibility Certificate Course Python Apps on AWS Course AWS Training Courses
Data Analytics
DSA Exam Data Analytics Course NumPy Course Pandas Course Excel Certificate Social Media Course What is a Certificate? ×
All Our Services
Services filter input ×
W3Schools offers a wide range of services and products for beginners and professionals, helping millions of people everyday to learn and master new skills.
Free Tutorials
Enjoy our free tutorials like millions of other internet users since 1999
References
Explore our selection of references covering all popular coding languages
Create a Website
Create your own website with W3Schools Spaces - no setup required
Exercises
Test your skills with different exercises
Quizzes
Test yourself with multiple choice questions
Get Certified
Document your knowledge
Log in / Sign Up
Create a free W3Schools Account to Improve Your Learning Experience
Pathfinder & My Learning
Track your learning progress at W3Schools and collect rewards
Upgrade
Become a PLUS user and unlock powerful features (ad-free, hosting, support,..)
Where To Start
Not sure where you want to start? Follow our guided path
Code Editor (Try it)
With our online code editor, you can edit code and view the result in your browser
Videos
Learn the basics of HTML in a fun and engaging video tutorial
Templates
We have created a bunch of responsive website templates you can use - for free!
Web Hosting
Host your own website, and share it to the world with W3Schools Spaces
Create a Server
Create your own server using Python, PHP, React.js, Node.js, Java, C#, etc.
How To's
Large collection of code snippets for HTML, CSS and JavaScript
CSS Framework
Build fast and responsive sites using our free W3.CSS framework
Browser Statistics
Read long term trends of browser usage
Typing Speed
Test your typing speed
AWS Training
Learn Amazon Web Services
Color Picker
Use our color picker to find different RGB, HEX and HSL colors.
Code Game
W3Schools Coding Game! Help the lynx collect pine cones
Set Goal
Get personalized learning journey based on your current skills and goals
Newsletter
Join our newsletter and get access to exclusive content every month
For Teachers
Contact us about W3Schools Academy for educational institutions
For Businesses
Contact us about W3Schools Academy for your organization
Contact Us
About sales: sales@w3schools.com About errors: help@w3schools.com
× ❮ HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE ❯
API Canvas API Console API Fetch API Fullscreen exitFullscreen() fullscreenElement fullscreenEnabled() requestFullscreen() API Geolocation coordinates getCurrentPosition() position API History API MediaQueryList API Storage clear() getItem() key() length removeItem() setItem() API Validation API Web crypto.getRandomNumber()
HTML Objects
a abbr address area article aside audio b base bdo blockquote body br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form head header h1 - h6 hr html i iframe img ins input button input checkbox input color input date input datetime input datetime-local input email input file input hidden input image input month input number input password input radio input range input reset input search input submit input text input time input url input week kbd label legend li link map mark menu menuitem meta meter nav object ol optgroup option output p param pre progress q s samp script section select small source span strong style sub summary sup table tbody td tfoot th thead tr textarea time title track u ul var video
button onclick= myFunction() Click me /button Try it Yourself
More examples below.
Description
The onclick event occurs when the user clicks on an HTML element .
Mouse Events
Event Occurs When onclick The user clicks on an element oncontextmenu The user right-clicks on an element ondblclick The user double-clicks on an element onmousedown A mouse button is pressed over an element onmouseenter The pointer is moved onto an element onmouseleave The pointer is moved out of an element onmousemove The pointer is moving over an element onmouseout The mouse pointer moves out of an element onmouseover The mouse pointer is moved over an element onmouseup The mouse button is released over an element
See Also:
The Mouse Event Object
Tutorial:
JavaScript Events
Syntax
In HTML:
element onclick= myScript Try it Yourself
In JavaScript:
object .onclick = function(){ myScript }; Try it Yourself
In JavaScript, using the addEventListener() method:
object .addEventListener( click , myScript ); Try it Yourself
Technical Details
Bubbles: Yes Cancelable: Yes Event type: MouseEvent Supported HTML tags: All except: base , bdo , br , head , html , iframe , meta , param , script , style , and title
More Examples
Click a button to display the date:
button onclick= getElementById('demo').innerHTML = Date() What is the time? /button Try it Yourself
Click a h3 element to change the text color:
h3 id= demo onclick= myFunction() Click me to change my color. /h3
script function myFunction() { document.getElementById( demo ).style.color = red ; } /script Try it Yourself
Another example on how to change the color of an element:
h3 onclick= myFunction(this, 'red') Click me to change my color. /h3
script function myFunction(element, color) { element.style.color = color; } /script Try it Yourself
Click to copy text from one input field to another:
button onclick= myFunction() Copy Text /button
function myFunction() { document.getElementById( field2 ).value = document.getElementById( field1 ).value; } /script Try it Yourself
How to assign an "onclick" event to the window object:
window.onclick = myFunction;
function myFunction() { document.getElementsByTagName( BODY )[0].style.backgroundColor = yellow ; } Try it Yourself
HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples
Get Certified
HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate FORUM ABOUT ACADEMY W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use , cookie and privacy policy .
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS .