Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use onclick method in javascript with Example
5649 Views
Example
<button onclick="javascript:do_it();">Click Me</button>

function do_it(){
    alert("You clicked Me!");
}
Element.onclick = function(){} 
onclick is a method of element object in javascript. onclick takes call back function as value to the event.
Example
<button id="my_button">Click Me</button>

document.getElementById("my_button").onclick = function(){
    alert("You clicked Me!");
};
Demo 
Another way using button events with Javascript 
<button onclick="javascript:do_it();">Label</button>
onclick="javascript:do_it()" attribute specified in button element to call do_it method while clicking on button.
Best Lessons of "Good Javascript Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details