-2

I have problems calling functions "onclick".

Is it appropriate to use this methodology? It tells me (function name) that it is not defined. While on other pages it works for me. I am learning. I have read that it is "bad" to use "onclick" and that it is better to use: $(document).on('click', '#id', function (){

If I use "$(document).on('click..." never problem. However, it suits me to "call" the function since this way I can pass parameters. Is there a way for "onclick" to work fine? In almost all popular browsers? What does it depend on if it works well? What matters because it fails?

ksav
  • 20,015
  • 6
  • 46
  • 66
motocross
  • 31
  • 5

1 Answers1

0

If you want to keep using onclick, it allows for variables.

For example, this works

<html>
<head></head>
<body onclick="alert('bruh')">x</body>
</html>

However, I think the current standard is to use event listeners. So in that case, it would be

document.body.addEventListener('click',()=>{alert("bruh")})
<html>
<head></head>
<body>x</body>
</html>
  • Yes, but calling functions (I don't know if the term "call" is correct). The truth, I was 1 hour trying to see why it marked me (function name) as "undefined". I couldn't see what the error was, but now it works for me. Between copy and copy desasher and undo, I lost and couldn't see the fault. – motocross May 30 '21 at 05:12
  • Between copy and paste everything, in the head, below, I use jquery, I don't know what I did, but now it works for me, as I said, between ctrl + z and ctrl + y and, in one way or another it worked. I have seen if, in the forum, that the exact same thing happened to others, marks the function as "undefined" – motocross May 30 '21 at 05:13