Posted on 2012-08-22
Last Modified: 2012-09-10
Ive got a load of divs on a page and would like to store some information on the individual divs, that I can pull in through a javascript function.

My only idea Ive got is to use the ID such as divButton_4322323_233422_31123 and then split the id using _. So if I needed to find 4322323 I could use
var parts = id.split("_") 

Open in new window

and then use

Open in new window

Does anyone else have any suggestion on storing information inside a div?

Thank you
Question by:tonelm54
    LVL 58

    Expert Comment

    No, if thats all you are wanting to do though you're question isn't entirely clear...
    LVL 49

    Accepted Solution

    use the data- attribute notation as per HTML 5 i.e

    <div data-id="1232" data-someothervalue="fred"></div>

    Open in new window

    LVL 53

    Expert Comment

    If you are trying to do something like pass a variable then just use a custom attribute:

    <div id="theid" custom="yourdata">

    then in the script


    not very elegant, and not a good practice but ...

    LVL 49

    Expert Comment

    by:Julian Hansen
    @CD& - best to use the new html5 convention which is to prefix any custom attributes with 'data-' as per post above.

    As of HTML5 it is good practice - convention was provided specifically for custom storing of data in elements to prevent having messy code as suggested by the author.
    LVL 53

    Expert Comment

    Yeah I didn't see your post or I would not have posted.  I agree data- is appropriate.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    Suggested Solutions

    Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
    This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
    In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now