Why don't you call the onclick function when I click on this checkbox?

function click()
{
  alert("function is called");
}
      

<form action="regCheck.aspx" method="post" onsubmit="return submitF()">
  <input type="checkbox" name="checkboxs" onclick="click()"/>
</form>
      

Run codeHide result


This is not my real file. I am just writing this part of my code so you can understand why my onclick function is not being called. If javascript opens an alert box when I click this checkbox, then I can use it. I hope you can help me. Thank!

+3


source to share


2 answers


Just change the name of your function from "click"

to a different name so that it is not obscured by the inline click

input function
):

function notcalledclick()
{
  alert("function is called");
}
      

<form action="regCheck.aspx" method="post" onsubmit="return submitF()">
  <input type="checkbox" name="checkboxs" onclick="notcalledclick()"/>
</form>
      

Run codeHide result




This is another reason to avoid adding inline javascript and prefer addEventListener

:

document.getElementById('someId').addEventListener('click', function(){
  alert("function is called");
});
      

<form action="regCheck.aspx" method="post" onsubmit="return submitF()">
  <input id=someId type="checkbox" name="checkboxs"/>
</form>
      

Run codeHide result


+2


source


Your function name should change.



There is already a function called click that is responsible for invoking the event handler. By declaring something else, you are overriding the first, so the event no longer works.

+1


source







All Articles