Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 538223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:00:32+00:00 2026-05-13T10:00:32+00:00

Sometimes I want to attach some kind of meta data to a HTML node

  • 0

Sometimes I want to attach some kind of meta data to a HTML node and I wondered, what’s the best way to do this.

I can imagine the following:

  • Non-standard attributes: <div myattr1="myvalue1" myattr2="myvalue2" > (Breaks validation)
  • Re-use an existing attribute <div class="myattr1-myvalue2-myattr2-myvalue2"> (Requires parsing and some level of escaping)

Both solutions are really ugly!

Is there any way to do this more elegantly? I’m already using jQuery so any good Javascript solution, if any, is appreciated, too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:00:32+00:00Added an answer on May 13, 2026 at 10:00 am

    HTML5 introduces the notion of custom data attributes, that anyone can create in order to attach custom, hidden data to elements for scripting purposes. Just create an attribute using a data- prefix, such as data-myattr1 or data-myattr2, and fill it with your data.

    <div data-myattr1="myvalue1" data-myattr2="myvalue2">Content</div>
    

    The nice thing about this solution is that it already works in all major browsers; they will all parse unknown attributes, and expose them in the DOM for access by JavaScript. HTML5 adds a few convenience mechanisms for accessing them, which haven’t been implemented yet, but you can just use the standard getAttribute to access them for now. And the fact that they are allowed in HTML5 means that your code will validate, as long as you’re willing to use a draft standard as opposed to an accepted one (I don’t believe the data- attributes are particularly controversial, however, so I’d be surprised if they were removed from the standard).

    The advantage this has over namespaced attributes in XHTML is the IE doesn’t support XHTML, so you would have to implement something that pretends to use namespace attributes but actually just uses invalid attributes with a : in their name, which is how IE would parse them. It’s nicer than using class, because putting lots of data into a class attribute overloads it quite a lot, and involves having to do extra parsing to pull out different pieces of data. And it’s better than just making up your own (which will work in current browsers), because it’s well defined that these attributes prefixed with data- are private pieces of data for scripting, and so your code will validate in HTML5 and won’t ever conflict with future standards.

    Another little-known technique for adding custom data to HTML, which is valid even in HTML 4, is adding script elements with type attributes of something other than text/javascript (or one of the couple of other types that can be used to specify JavaScript). These script blocks will be ignored by browsers that don’t know what to with them, and you can access them via the DOM and do what you want with them. HTML5 explicitly discusses this usage, but there’s nothing that makes it invalid in older versions, and it works in all modern browsers as far as I know. For example, if you would like to use CSV to define a table of data:

    <div>
      <script type="text/csv;header=present">
        id,myattr1,myattr2
        something,1,2
        another,2,4
      </script>
      Content
    </div>
    

    This is the technique used by SVG Web to allow embedding of SVG in HTML, with emulation via Flash if the browser doesn’t support native SVG. Currently, even browsers that do support SVG (Firefox, Safari, Chrome, Opera) don’t support it directly inline in HTML, they only support it directly inline in XHTML (because the SVG elements are in a different namespace). SVG Web allows you to put SVG inline in HTML, using a script tag, and it then transforms those elements into the appropriate namespace and adds them to the DOM, so they can be rendered as XHTML. In browsers that don’t support SVG, it then also emulates the function of the elements using Flash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data in a chart using SSRS, sometimes we want to see
If I'm not online, I sometimes want to package some changes to a commit,
Windows's Snipping tool can capture the screen, but sometimes I want to capture the
Sometimes I want to know what column my cursor is at, so I can
Sometimes I want to join a record that I can easily identify as sort
This is my code: def execute(f, *args): f(args) I sometimes want to pass no
Sometimes I want to build Python or GCC from scratch just for fun, but
When using powershell, sometimes I want to only display for example the name or
I have a flash program that loads movie clips dynamically and sometimes they want
As I learn new features in Oracle, sometimes I want to test how a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.