Can't use string in event attributes #973
-
In docs written that script templates are a legacy feature. I wanted to call a function with a value from template but it says that
|
Beta Was this translation helpful? Give feedback.
Answered by
pharrisee
Oct 28, 2024
Replies: 2 comments 3 replies
-
There may be a better answer to your issue out there, but something like this works for me: <p data-id="11" class="clickable">
Welcome to the index page
</p>
<script>
// could also select a single element here and forgo the loop
let ps = document.getElementsByClassName('clickable');
for (let p of ps) {
p.addEventListener('click', function(e) {
let pid= p.getAttribute('data-id');
console.log('clicked ' + pid, e.target)
})
}
</script> Not as clean as using a ComponentScript but it works well. |
Beta Was this translation helpful? Give feedback.
2 replies
-
To get even more subversive this also works: <div { templ.Attributes{"onclick":fmt.Sprintf("doIt(%d)",11)}... }>
<p>click me</p>
</div> The more ways I find of misusing templ.Attributes the dirtier I feel. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
MilkeeyCat
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To get even more subversive this also works:
The more ways I find of misusing templ.Attributes the dirtier I feel.