JavaScript - Help me with easy program!

+1 Radek Vlcek · April 23, 2015
Hey, I want to have one button and program it via JS to pop up an alert message after clicking on it.
In .html file i have:

<input type="button" name="butty"/>

And in .js file I have:

function mainFun(){
    var x = document.getElementByName('butty');
    x.addEventListener('click', work, false);

function work(){

window.addEventListener('load', mainFun, false);

Please help me how to make it work, reply me with a correct piece of code.

Post a Reply


Oldest  Newest  Rating
0 Jason Cameron · April 24, 2015
<input id="myButton" type="button" name="butty"/>

function mainFun(){
var myBtn = document.getElementById('myButton');

myBtn.addEventListener('click', alertHandler);

function alertHandler(){
alert("hello i am here");

here i gave you input element an id of "myButton"
then in js i get it through the dom and place it in a var called myBtn;
gave it an event listener and made the function..
0 Radek Vlcek · April 27, 2015
Thanks guys, it works now. My only mistake was referencing it by name.
  • 1



A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.