Tech Study

Javascript Onclick Event

What is javascript onclick event

javascript onclick event occurs when the user click on element .When element gets clicked, javascript onclick function help us to execute the function . This event can be added to any HTML element, such as a button, link, or image.

Supported HTML tag :

It support all HTML tag except: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.

Syntax for javascript onclick event :

To add  javascript onclick event on an element, we can use the element. onclick property, or the addEventListener() method.

object.onclick = function(){myScript};

Example for onclick function in javascript |Button onclick javascript

Following is an example of using javascript onclick function to execute  function when button get clicked, the button tag is created using HTML and then a JavaScript script is written to provide the ‘onclick’ property to the button -:

Example :

This can be used to create a button which when clicked will show an alert saying “Here is the onclick effect!!”

<button id="Btn">Click me</button>


  document.getElementById("Btn").onclick = function() {

    alert("Here is the onclick effect!!");



Alternatively, you can use the addEventListener() method to attach the javascript onclick event to an element:

<button id="Btn">Click me</button>


  var btn = document.getElementById("Btn");

  btn.addEventListener("click", function() {

    alert("Again the onclick effect!");



Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more