The community member had a question about using the onclick attribute to call a JavaScript function within an embed element. They provided an example of a simple function that changes the text color of an element when clicked. However, they encountered an error stating that the function was not defined. The comments suggest that using the onclick attribute is considered "old school" and that it's better to use addEventListener instead. One community member provided a solution using addEventListener, which resolved the issue. The consensus from the comments is that the onclick attribute is not recommended for modern web development.
I've some question about an onclick attributes. Is it possible to have onclick = myFunction() and call that function in a script in an embed element? Seems not working. I would love to have that feature π
I'd like to implement a simple function like that :
<p id="demo" onclick="myFunction()">Click me to change my text color.</p>
<script> function myFunction() { document.getElementById("demo").style.color = "red"; } </script>
I've insert it in a HTML embed element, but I've got this error : myFunction() is not define. Is it related to the onclick or not?
I've insert it in a HTML embed element, but I've got this error : myFunction() is not define. Is it related to the onclick or not?Thanks for your help!","url":"https://help.webstudio.is/attribute-onclick-doesnt-work-fppt0b0lrYN1","identifier":"fppt0b0lrYN1","publisher":{"@type":"Organization","name":"Webstudio","logo":{"@type":"ImageObject","url":"https://assets.usehall.com/org_01JEYTC8WWMF7N46W9J4J7CFDP/c7f10f06-f934-4e35-b008-431a89254f9f.png"}},"comment":[{"@type":"Comment","text":"It's old school. Nobody does this any more, use addEventListener instead, but yes it does work","dateCreated":"2024-07-26T09:18:41.133Z","dateModified":"2024-07-26T09:18:41.133Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"},"commentCount":0,"comment":[],"position":1,"upvoteCount":0},{"@type":"Comment","text":"We don't do anything special there","dateCreated":"2024-07-26T09:18:58.316Z","dateModified":"2024-07-26T09:18:58.316Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"},"commentCount":0,"comment":[],"position":2,"upvoteCount":0},{"@type":"Comment","text":"Is your script having run on canvas?","dateCreated":"2024-07-26T09:22:01.636Z","dateModified":"2024-07-26T09:22:01.636Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"},"commentCount":0,"comment":[],"position":3,"upvoteCount":0},{"@type":"Comment","text":"All right, I made a change :It's working fine right now. Thanks a lot @Oleg Isonen !Just to understand \"onclick\" attribute is old school? Not good to use it?","dateCreated":"2024-07-26T09:39:18.773Z","dateModified":"2024-07-26T09:39:18.773Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/a5bb43e0-c553-447b-82cc-c4c4177bf6b9","name":"Milan Boisgard | Uncode School","identifier":"a5bb43e0-c553-447b-82cc-c4c4177bf6b9","image":"https://cdn.discordapp.com/avatars/378836225009647628/67e9e7df4880d828a727cc3efba93c81.webp?size=256"},"commentCount":0,"comment":[],"position":4,"upvoteCount":0},{"@type":"Comment","text":"yes","dateCreated":"2024-07-26T09:45:05.064Z","dateModified":"2024-07-26T09:45:05.064Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"},"commentCount":0,"comment":[],"position":5,"upvoteCount":0}],"author":{"@type":"Person","url":"https://help.webstudio.is/members/a5bb43e0-c553-447b-82cc-c4c4177bf6b9","name":"Milan Boisgard | Uncode School","identifier":"a5bb43e0-c553-447b-82cc-c4c4177bf6b9","image":"https://cdn.discordapp.com/avatars/378836225009647628/67e9e7df4880d828a727cc3efba93c81.webp?size=256"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0}}]