Deka87 Deka87 - 1 year ago 62
jQuery Question

Use function to dynamically update another function's arguments object

I use a simple plugin to add some functionality to my template:

lines: 3

What I want to indicate a number of lines to show with a function instead of a number:

lines: function() { return 3 }

This doesn't work for some reason. I am pretty sure this is not the plugins fault, but my misunderstanding of how it works. Any ideas?

A codepen is at:

Answer Source

For that code to work, the plugin would have to be built to accept a function as a value for the lines property. From the source, the only valid values for lines that I note are the string "auto" or a numeric value.

You would need to do something like this:

$('.foo').each(function() {
   $(this).ellipsis({ lines: $(this).data('truncate-lines') });

If you want to modify the plugin to support your code, adding the following as the first lines of code in init() would probably do the trick:

if(typeof base.opts.lines === 'function') {
   base.opts.lines =;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download