josephschmitt/Clamp.js

Using Clamp.js with jQuery, possible?

drcd opened this issue · 9 comments

drcd commented

I have a table, and inside each rows there's a single <td> element with the class "clampThis" that I'd like to clamp the text within.

So I thought something like this would work

var item = $('.clampThis');
$clamp(item, {clamp: 1, useNativeClamp: false});

But I get this error instead:

Uncaught TypeError: Cannot read property 'webkitLineClamp' of undefined clamp.min.js:11

Maybe I'm doing it the wrong way? I'm not sure. I'm not a great JavaScript wizard, so help would be much appreciated. :)

This happens on Google Chrome (Version 31.0.1650.63 m) on Windows 7.

I just had the same problem. It works if you do this:

$('.clampThis').each(function(index, element) {
    $clamp(element, { clamp: 1, useNativeClamp: false });
});

It looks like the problem is clamp doesn't know how to take in a list of elements. Instead, I had to use each to call clamp on each individual element.

+1

@bgreeley's method worked for me as well. Thanks!

+1 for @bgreeley answer

Hi. I have a very simple problem to which I have not found the solution yet :). Does clamp.js work correctly if I have, for instance, just one very long word (german language is the target with its horrifying long words) ?. I mean, I have configured clamp.js to cut away anything that does not fit on a 3-line text, but on the third line I have only a very long word that is not cut away.

Thanks.
M.

+1 for @bgreeley the for each worked for me too. Thank you!

Try setting native element instead of jquery element. Worked for me.

var item = $('.clampThis');
$clamp(item[0], {
  clamp: 1
});

+1 for @bgreeley , thanks!

Does Anyone succeeded to use this in *Firefox *?

@hgsadhrakiya firefox is here: #30

Check link below for FF fix:
https://github.com/jmenglis/clamp-js-main