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
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.
Below given are the examples of the onclick button in html:
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.
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