[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now