Cannot reach $ (this) from $ (). Data ()

I am trying to set the class of my element inside the data object of the same element, but it continues to return 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 orgClass.

+3
source share
2 answers
var me = $(this);
me.data({
  orgSize  : { width:me.width(), height:me.height() },
  orgClass : function(){
    cl = me.attr('class');
    return cl ? (' '+cl) : ' somethingelse';
  }
});
+6
source

its volume problem, 'this' inside "orgSize" refers to the "orgSize" object itself.

0
source

Source: https://habr.com/ru/post/1780713/


All Articles