<script> tag. But, do you know that you can put custom attributes and access it inside your script?
For example, let’s take a look at this giscuss.app script tag:
Line 2 until 12 are called dataset. It’s a custom attributes that you can put to HTML elements. As long as you use
data- as a prefix, it’s considered as dataset. You can also put your custom elements/attributes without
data- prefixed, but it’s just an attribute, not dataset. Now, how do you fetch the attributes value?
Let’s say you have
index.js script that you import to your code like below:
attr4 you can do this inside
As for the dataset, the
data- prefix will be removed. But for the rests, it is considered as common attributes just like
Now you can get your user input from attributes, just think of it like function arguments. It’s simple and straightforward. Although you can choose between 2 methods above, I recommend you to use the
dataset since it is more semantic than just a plain attribute.
Thank you for reading!