Using onClick in JavaScript (UOJ)


UOJ:   SOLD       




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 when the button is clicked, the function is not called. What am I doing wrong here?

Solutip

You need to make a slight revision to the following code:

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

Become:

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

You should rename your function. The function  click() is already defined on the button (simulating a click), and has higher priority than your method.

Note that some of the advice here is wrong, and you shouldn't spend much time on it:

  • Don't use  onclick="javascript:myfunc()". Only use the javascript: prefix inside  href the hyperlink: attribute  <a href="javascript:myfunc()">.
  • You don't have to end with a semicolon.  onclick="foo()" and  onclick="foo();" both work fine.
  • Event attributes in HTML are not case sensitive, so onclick, onClick and ONCLICK all work. It is common practice to write the attribute in lower case: onclick. note that javascript itself is case sensitive, so if you write  document.getElementById("...").onclick = ...,then it should be all lower case.

Post a Comment

Previous Next

نموذج الاتصال