javascripthtmldom-eventsevent-driven

onclick event function in JavaScript


I have some JavaScript code in an HTML page with a button. I have a function called click() that handles the onClick event of the button. The code for the button is as follows:

<input type="button" onClick="click()">button text</input>  

The problem is that when the button is clicked, the function is not called. What am I doing wrong here?


Solution

  • Two observations:

    1. You should write

      <input type="button" value="button text" />
      

      instead of

      <input type="button">button text</input>
      
    2. You should rename your function. The function click() is already defined on a button (it simulates a click), and gets a higher priority then your method.

    Note that there are a couple of suggestions here that are plain wrong, and you shouldn't spend to much time on them: