j2html icon indicating copy to clipboard operation
j2html copied to clipboard

Question: javascript and common events

Open Delta-38 opened this issue 6 years ago • 3 comments

I might be missing the point, but I didn't see it the examples section. What is the recommended approach with j2html for common DOM events? Such as onclick, onhover etcetera I find that I can achieve that with attr(name,value) but it doesn't show up intuitively (or rather I find myself trying lots of things before remembering.

Thanks in advance for creating such a fun library to work with in any case.

Delta-38 avatar Sep 03 '19 10:09 Delta-38

Hi! J2html is just for creating html, I recommend you keep your Javascript in a separat file and include it via a script tag.

tipsy avatar Sep 13 '19 19:09 tipsy

Try HTMX.

jankod avatar Sep 01 '21 13:09 jankod

I got here by searching exactly for this same, I think it does makes sense to have on click for buttons, as is it is still achievable to add them using the following syntax

button().attr("onClick=console.log('hi')")

Non the less having something as:

button().onClick("console.log('hi')")

would be nice preventing typos at least on the on click part and also facilitating using reflection to build html on the fly calling certain functions.

I also agree that js should be separate yet the calling to it can be done on HTML, this way preventing the need of having to do the linking on JS itself

Ordiel avatar Jun 07 '22 20:06 Ordiel