Join the Webstudio community

Updated 4 weeks ago

Attribute "onclick" doesn't work

Hi Team,

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?

Thanks for your help!
Attachment
SCR-20240726-jmca.jpeg
O
M
5 comments
It's old school. Nobody does this any more, use addEventListener instead, but yes it does work
We don't do anything special there
Is your script having run on canvas?
All right, I made a change :

<script>
document.getElementById("demo").addEventListener("click", function() {
this.style.color = "red";
});
</script>

It's working fine right now. Thanks a lot @Oleg Isonen !

Just to understand "onclick" attribute is old school? Not good to use it?
Add a reply
Sign up and join the conversation on Discord
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!","answerCount":5,"upvoteCount":0,"datePublished":"2024-07-26T08:24:36.247Z","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"},"suggestedAnswer":[{"@type":"Answer","text":"It's old school. Nobody does this any more, use addEventListener instead, but yes it does work","upvoteCount":0,"dateCreated":"2024-07-26T09:18:41.133Z","datePublished":"2024-07-26T09:18:41.133Z","dateModified":"2024-07-26T09:18:41.133Z","url":"https://help.webstudio.is/attribute-onclick-doesnt-work-fppt0b0lrYN1#a4672819-b9a3-4e58-943b-e717983539ca","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"}},{"@type":"Answer","text":"We don't do anything special there","upvoteCount":0,"dateCreated":"2024-07-26T09:18:58.316Z","datePublished":"2024-07-26T09:18:58.316Z","dateModified":"2024-07-26T09:18:58.316Z","url":"https://help.webstudio.is/attribute-onclick-doesnt-work-fppt0b0lrYN1#0d6057d4-a172-413b-a20b-e0c01ef4eb9d","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"}},{"@type":"Answer","text":"Is your script having run on canvas?","upvoteCount":0,"dateCreated":"2024-07-26T09:22:01.636Z","datePublished":"2024-07-26T09:22:01.636Z","dateModified":"2024-07-26T09:22:01.636Z","url":"https://help.webstudio.is/attribute-onclick-doesnt-work-fppt0b0lrYN1#525cf562-4667-49b3-a5e1-e3e0fd19f5ba","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"}},{"@type":"Answer","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?","upvoteCount":0,"dateCreated":"2024-07-26T09:39:18.773Z","datePublished":"2024-07-26T09:39:18.773Z","dateModified":"2024-07-26T09:39:18.773Z","url":"https://help.webstudio.is/attribute-onclick-doesnt-work-fppt0b0lrYN1#d0ea827f-790e-4f9f-9e44-56833377bbf8","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"}},{"@type":"Answer","text":"yes","upvoteCount":0,"dateCreated":"2024-07-26T09:45:05.064Z","datePublished":"2024-07-26T09:45:05.064Z","dateModified":"2024-07-26T09:45:05.064Z","url":"https://help.webstudio.is/attribute-onclick-doesnt-work-fppt0b0lrYN1#71bfe146-6338-4b06-a1aa-8e5ddb73e7f3","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"}}]}}