0

I am trying to use offsetHeight to find the height of an element based on its class, so far I can find the height based on div id, but what if I have multiple classes assigned to the same id?

I adapted this jsfiddle to exemplify what I'm talking about: http://jsfiddle.net/VvrAs/80/

var idheight = $('textarea').offsetHeight;
    var classheight1 = $$('.field').offsetHeight;
    var classheight2 = $$('.field2').offsetHeight;

where field and field2 are div classes, and textarea is a div id

I want the undefined values to give the height of the div based on class.

Thanks.

1 Answers1

0

The $$ selector behaves much like querySelectorAll, in that it will not return a single element, but a NodeList containing multiple elements, even when only one element matches the query.

NodeLists can be traversed like Arrays, so in your case:

var classheight1 == $$('.field')[0].offsetHeight;
var classheight2 == $$('.field2')[0].offsetHeight;

In essence, when you originally called $$('.field'), you asked MooTools to find all instances of elements containing the class field. It returned a NodeList of 1 item, but you'll still need to reference it via its array index.

Julian H. Lam
  • 25,501
  • 13
  • 46
  • 73