Error in onclick

Asked

Viewed 197 times

1

I have a form I call the function mail() at the onclick event.

<input type="checkbox" name="MesmoEmail" value="S" id="mesmoEmail" onclick="mesmoEmail();"> Mesmo e-mail do cadastro

And Javascript is like this:

<script>
function mesmoEmail(){
  alert('aqui');
  var marcado = document.getElementById('mesmoEmail');
}
</script>

The problem is that it is not working and in the console appears the following error:

inserir a descrição da imagem aqui

And in the source code:

inserir a descrição da imagem aqui

So far when I put the code, the word Even is of another color. I am not able to identify the error.

2 answers

1


I identified the bug. The ID has the same function name in onclick, so I made the following change:

<input type="checkbox" name="mesmoEmail" value="S" id="mesmoEmail" onclick="mesmoEmail();"> Mesmo e-mail do cadastro

To:

<input type="checkbox" name="MesmoEmail" value="S" id="mesmoemail" onclick="mesmoEmail();"> Mesmo e-mail do cadastro
  • 1

    Oops, I put an answer and I didn’t see that I had answered. haha

  • 1

    Another question about this problem of how Ids can cause problems is: https://answall.com/a/123106/129

1

You can simplify it this way:

function mesmoEmail(el){
  var marcado = el.value;
  console.log(marcado);
}
<input type="checkbox" name="MesmoEmail" value="S" id="mesmoEmail" onclick="mesmoEmail(this);"> Mesmo e-mail do cadastro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.