Pichi Wuana
Pichi Wuana

Reputation: 752

Why when I click on the checkbox doesn't call the function onclick?

function click()
{
  alert("function is called");
}
<form action="regCheck.aspx" method="post" onsubmit="return submitF()">
  <input type="checkbox" name="checkboxs" onclick="click()"/>
</form>

This is not my real file. I'm just writing this part of my code so you people could see why my onclick function is not being called. If the javascript opens a window of alert when I click the checkbox, then it means I can use it. I hope you can help me. Thanks!

Upvotes: 3

Views: 439

Answers (2)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382092

Just change the name of your function from "click" to another name, so that it's not shadowed by a the native click function of the input):

function notcalledclick()
{
  alert("function is called");
}
<form action="regCheck.aspx" method="post" onsubmit="return submitF()">
  <input type="checkbox" name="checkboxs" onclick="notcalledclick()"/>
</form>

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>

Upvotes: 2

R4nc1d
R4nc1d

Reputation: 3113

Your function name should change.

There's already a function called click, responsible for calling the event handler. By declaring another, you override the first, so the event doesn't work anymore.

Upvotes: 1

Related Questions