Dino Geek, try to help you

How to use custom data (data-*) in HTML?


Custom data attributes in HTML, also known as data-\*, allows you to store extra information or data in your HTML tags. They are used to store custom data private to the web page or application. They give us the ability to embed custom data attributes on all HTML elements.

Here is how to use them:

1. Insert the custom data: To add a custom data attribute, you typically begin it with “data-” and then follow it with your preferred name.

For example:

```

Content goes here…

```

In this case, `custom-name` is the custom data attribute name and `myvalue` is its value.

1. Access the custom data with JavaScript: JavaScript’s `dataset` property can be used to read the custom data.

For example:

```
var div = document.getElementById(“example”);
var customNameValue = div.dataset.customName;
```

In this case, `dataset.customName` is used to get the value of the custom data attribute `data-custom-name`.

1. Access the custom data with jQuery: jQuery’s `data()` method can be used to read the custom data attribute.

For example:

```
var customNameValue = $(“#example”).data(“custom-name”);
```

In this case, `.data(“custom-name”)` is used to get the value of the custom data attribute `data-custom-name`.

1. Modify the custom data: Same methods can be used to modify the data.

For instance:

```
div.dataset.customName = “newvalue”; // JavaScript
$(“#example”).data(“custom-name”, “newvalue”); // jQuery
```

In these cases, the value of `data-custom-name` is changed to `newvalue`.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use