Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Object #<HTMLDivElement> has no method 'height' - Can't call jquery method on element

Tags:

jquery

I would need to loop within some html elements, using the same code I get an error

jquery Uncaught TypeError: Object # has no method 'height'

What is wrong here?

clamp: function() {
  var elms = $('#wrapper .content ul li .title');
  elms.each(function(i) {
    debugger
    var elm = this;
    var h = elm.height();
    var eO = elm.outerHeight();
    if (eO > h) {
      elm.text(function(index, text) {
        return text.replace(/\W*\s(\S)*$/, '...');
      });
    }
  })
like image 883
GibboK Avatar asked Oct 02 '13 09:10

GibboK


2 Answers

In your each() method, this refers to the DOM element, not a jQuery object (hence you can't call jQuery methods on it). Instead of

elm = this

Try

elm = $(this)
like image 113
George Avatar answered Oct 18 '22 23:10

George


You need to wrap the DOM element (this) into a jQuery object:

var elm = $(this);

See the docs of the .each() function:

More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

like image 39
Wolfram Avatar answered Oct 18 '22 23:10

Wolfram