SO - 30235768

by Arun P Johny

HTML

<div id="test"></div>

JavaScript

$(function () {

    var carousel = function () {
        this.prop1 = 1;
        this.prop2 = 'two';
        this.prop3 = 3;
    }

    var _str = $('#test'),
        $str_data = _str.data();

    console.log($str_data);

    var data = _str.data('carousel');

    if (!data) {
        //create a new carousel and assign it to data so that it gets a new value
        data = new carousel();
        //store the new carousel value
        _str.data('carousel', data);
    }

    console.log(data);

    if (!data) {
        console.log('no data');
    }

});