HTML onclick Button
In HTML, we have a button for submitting the user-request data to the server(backend) to validate and navigate the web pages. Mainly, if we use the onclick button for event attributes and is supported by all the browsers, So is the browser compatibility feature wherever we use this event function in our scripts. The event appears when the user clicks on the
Working of onclick Button
Mainly it will be used for triggering and call the function wherever the user needs to click on the button. If the user clicks on the mouse through the
Syntax:
<button name="" value="" onclick<strong> ="</strong>function()"/>
The above syntax is the basic usage of the onclick event in the html attributes. We also customized the event wherever we need which is the user requirements.
Examples of HTML onclick Button
Below given are the examples of the onclick button in html:
Example #1
Code:
<html> <body> <button onclick="Function()">Click</button> <p id="sample"></p> <script> function Function() { document.getElementById("sample").innerHTML = "Welcome"; } </script> </body> </html>
Output:
In the above example, we have created the javascript function; additionally, when the user clicks the button “click”, it will display the value “Welcome” in the browser itself.
Example #2
Code:
<html> <body> <p id="sample" onclick="Function()">Click</p> <script> function Function() { document.getElementById("sample").innerHTML = "Welcome"; } </script> </body> </html>
Output:
The above example is also the same as we discussed in the previous example 1, but here we are not using any tag(paragraph) tag. So it will reduce the lines of code. Code: Output: In the example above, we called the JavaScript function to copy the values from username to password after selecting the “click” button that is automatically copied from username to password. It is one of the basic operations for the onclick event. For Example, if we want to change the colors of the given values after a click. Output: Here, a couple of suggestions that need to be helpful for using the onclick event in the html tags. 1. Do not use the onclick=”javascript:function()”,only use the javascript : like prefix inside the attribute like href hyperlink: 2. We don’t end with semicolon like onclick=” function()” and onclick=” function();” both will be work fine, but it’s not a good practice for using semicolon for function ends. 3. Event attributes like onclick,onCLICK, and ONCLICK all will be work, but in common practice, we write the attributes like lowercase, even javascript itself case sensitive, when we write like document.getElementById().onclick=”, then all must be the lowercase. onclick is also the event trigger in the javascript functions; it may be helpful for user validations and navigate the web pages. In jquery, also we use the onclick event act as a major part of the user-defined requirements. Like, react js, angular are some other frameworks we use in onclick functions. It also supports most of the modern browsers nowadays like google chrome, Mozilla Firefox, and safari, etc. In javascript, we can handle not only onclick event functions, but it may also be used for some other attributes like “on select,onsubmit,ontoggle,onkeyup “, etc.. based on the user requirements, we can use the event attributes in the html. The above is the detailed content of HTML onclick Button. For more information, please follow other related articles on the PHP Chinese website!Example #3
<html>
<body>
Username: <input type="text" id="user" value="sivaraman" ><br>
Password: <input type="text" id="pass"><br><br>
<button onclick="Function()">Click</button>
<script>
function Function() {
document.getElementById("pass").value = document.getElementById("user").value;
}
</script>
</body>
</html>
OnClick Event in Various Events
<html>
<body>
<div id="example">Click</div>
<script>
document.getElementById('example').onclick = function changeContent() {
document.getElementById('example').innerHTML = "Welcome to my domain";
document.getElementById('example').style = "Color: green";
}
</script>
</body>
</html>
Conclusion

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

AI Hentai Generator
Generate AI Hentai for free.

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



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
