Proper Use Of HTML Data Attributes
        Posted  
        
            by 
                VirtuosiMedia
            
        on Programmers
        
        See other posts from Programmers
        
            or by VirtuosiMedia
        
        
        
        Published on 2012-09-23T00:18:44Z
        Indexed on 
            2012/09/23
            3:49 UTC
        
        
        Read the original article
        Hit count: 578
        
I'm writing several JavaScript plugins that are run automatically when the proper HTML markup is detected on the page. For example, when a tabs class is detected, the tabs plugin is loaded dynamically and it automatically applies the tab functionality. Any customization options for the JavaScript plugin are set via HTML5 data attributes, very similar to what Twitter's Bootstrap Framework does.
The appeal to the above system is that, once you have it working, you don't have worry about manually instantiating plugins, you just write your HTML markup. This is especially nice if people who don't know JavaScript well (or at all) want to make use of your plugins, which is one of my goals.
This setup has been working very well, but for some plugins, I'm finding that I need a more robust set of options. My choices seem to be having an element with many data-attributes or allowing for a single data-options attribute with a JSON options object as a value. Having a lot of attributes seems clunky and repetitive, but going the JSON route makes it slightly more complicated for novices and I'd like to avoid full-blown JavaScript in the attributes if I can. I'm not entirely sure which way is best.
- Is there a third option that I'm not considering?
- Are there any recommended best practices for this particular use case?
© Programmers or respective owner