CSS Tag

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
parts[1];

Open in new window


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

Thank you
tonelm54Asked:
Who is Participating?
 
Julian HansenCommented:
use the data- attribute notation as per HTML 5 i.e

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

Open in new window

0
 
GaryCommented:
No, if thats all you are wanting to do though you're question isn't entirely clear...
0
 
COBOLdinosaurCommented:
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

somevar=document.getElementById('thediv').custom;

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

Cd&
0
 
Julian HansenCommented:
@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.
0
 
COBOLdinosaurCommented:
Yeah I didn't see your post or I would not have posted.  I agree data- is appropriate.

Cd&
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.