Wednesday, May 23, 2012

jQuery convert data-* attributes to lower cammel case properties

I have the following jQuery script to intialise a jQuery plugin called poshytips. I want configure the plugin using Html5 data attributes. I am repeating myself big time, can anyone come up with a better way to do this?



$('.poshytip-trigger').each(function (index) {
var $this = $(this);
var data = $this.data();

var options = {};

if (data['class-name']) {
options.className = data['class-name'];
}

if (data['align-x']) {
options.alignX = data['align-x'];
}

if (data['align-y']) {
options.alignY = data['align-y'];
}

if (data['offset-y']) {
options.offsetY = data['offset-y'];
}

if (data['offset-x']) {
options.offsetX = data['offset-x'];
}

$this.poshytip(options);
});




No comments:

Post a Comment