Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot reach $(this) from within $().data()

I'm trying to set the class of my element inside a data object of the same element, but it keeps returning undefined.

$(this).data({
            orgSize:{ // Write all the sizes as data. For future reference.
                width: $(this).width(), //returns the width just fine!
                height: $(this).height() //returns the height just fine!
            },
            orgClass: function(){
                cl = $(this).attr('class');
                if(cl){
                    return ' ' + cl;
                }else{
                    return ' somethingelse';
                }
            } //returns undefined
        });

        console.log($(this).attr('class')) //returns the class

edit: The problem is within the orgClass.

like image 390
dubbelj Avatar asked Jan 01 '26 01:01

dubbelj


1 Answers

var me = $(this);
me.data({
  orgSize  : { width:me.width(), height:me.height() },
  orgClass : function(){
    cl = me.attr('class');
    return cl ? (' '+cl) : ' somethingelse';
  }
});
like image 162
Phrogz Avatar answered Jan 02 '26 16:01

Phrogz