DEV Community

AquaCat
AquaCat

Posted on

2

【JavaScript】How to embed script in eventhandler

When you want to use a JavaScript for event handler, you can embed the script directly as below.

<div id="element">Hello</div>
<button type="button" onclick="document.querySelector('#element').textContent='Clicked!'">Press Here</button>
Enter fullscreen mode Exit fullscreen mode

As you know, this also works.

<div id="element">Hello</div>
<button type="button" onclick="changeText()'">Press Here</button>
...
<script>
function changeText(){
document.querySelector('#element').textContent='Clicked!'
}
</script>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay