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

  • SEARCH
  • Home
  • 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 9133369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:28:37+00:00 2026-06-17T08:28:37+00:00

I need to access general information from my site using javascript. So far, I

  • 0

I need to access general information from my site using javascript. So far, I have the following options:

  • Using an html element :<input type="hidden" value="MyValue"/>

  • Using a custom attribute in an existing html element : <div id="HtmlElement" myattr="MyValue"></div> and then access it using document.getElementById("HtmlElement").getAttribute("myattr")

  • Using a data attribute in an existing html element: <div id="HtmlElement" data-myattr="MyValue"></div> and then access it using jQuery("#HtmlElement").data("myattr")

I was wondering what are the benefits of using either option.

I’m not a fan of using hidden inputs because I don’t like the idea of having a loose html element that contains information. But since I need it to display general information, not information related to an existing html element in the page, it doesn’t seem so bad.

On the other side, I’m not a fan of abusing the use of an external library but in my case I’m allready loading jQuery in my site, so it’s not as if i was loading an entire library just for this.

And finally, even dough performance is allways an issue, in my case it’s not gonna make much difference if it’s the fastest solution.

  • 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-06-17T08:28:38+00:00Added an answer on June 17, 2026 at 8:28 am

    I would go with data attributes because that’s what they are for and modern browsers have a native api for accessing them, while still allowing non-modern browsers to access them as custom attributes.

    given this html:

    <div data-foo="bar"></div>
    

    // modern browser:
    foo = document.getElementByID("myelementid").dataset.foo;
    
    // older browser:
    foo = document.getElementByID("myelementid").getAttribute("data-foo");
    
    // jQuery (all browsers)
    foo = $("#myelementid").data("foo");
    

    Note if your data attribute is data-foo-bar, the key in dataset and .data will be fooBar

    As sdespont mentions, the data should be relevant to the element you are storing it on.

    Update:
    It’s also important to note that you can also get the value of a data attribute using the .attr method, however there is a pretty important difference between the two. Getting a data attribute’s value with .data will attempt to parse the value of the attribute into the correct javascript type, for example, if it can be converted to an int, it will be converted to an int. If it can be converted into an object or an array, it will be converted to an object or an array. If you instead used .attr(), you can be sure that you will always have a string.


    Probably also worth mentioning that using .attr() should be prefered over .data() unless you specifically need the features given by .data() due to the fact that by using .data(), a data cache will be created for that element and it’s data, which isn’t needed unless you actually intend to use .data() multiple times or need the extra features provided by .data()

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

Sidebar

Related Questions

I need to access camera from a remote html page. I load this html
I need access to the uint64_t typedef from stdint.h in some wrapper code that
I have two different modules that need access to a single file (One will
I know that's bad design, but I need access to the view from my
I need to access a components tag attribute like: <h:inputtext id=input_age/> from a backing
I need to access the commandline from within a C# application. I can't find
i need to access folder in project .I have used Directory.CurrentDirectory and it is
I am developing a website using Asp.Net MVC framework. We need to add general
I have an application which need to access context in a lot of different
I am programming in python and need to access the name I have given

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.