0

In the below piece of code I am getting the JSLint error (Don't make functions within a loop). can you help me to modify the code to satisfy the JSLint.

setEllipsis : function () {
    $('.co_documentReportTable > thead > tr > th').each(function() {
        $(this).find('.co_dcrTable_Header').each(function() {
            var $el = $(this);
            if($el.css("overflow") === "hidden") {
                var text = $el.html();
                while ($el.height() > 64) {
                    $el.text(function (index, text) {
                        return text.replace(/\W*\s(\S)*$/, '...');
                    });
                    //
                    var txt = 'Fair consideration/no fraudulent conveyance';
                    if(text.indexOf(txt) !== -1 ) {
                        $el.text(txt + '...');
                    }
                }
            }
        });
    });
}

I tried creating another function and calling it then in that case while loop is getting executed forever.

3
  • this is for multiline and above code works but am getting jslint error how to resolve the jslint error ? Commented Jun 26, 2014 at 15:25
  • 1
    Search for [javascript] closure loop here on SO. Commented Jun 26, 2014 at 15:25
  • Just disable that rule. It hardly applies here. Commented Jun 26, 2014 at 15:32

1 Answer 1

3

Looks in general like you want to use the text-overflow: ellipses CSS property instead here.

Otherwise - pull the anonymous function up and give it a name, so you might end up with :

setEllipsis : function () {
    var addEllipses = function(index, text) {
        return text.replace(/\W*\s(\S)*$/, '...');
    };

    $('.co_documentReportTable > thead > tr > th').each(function() {
        $(this).find('.co_dcrTable_Header').each(function() {
            var $el = $(this);
            if($el.css("overflow") === "hidden")
            {
                var text = $el.html();
                while ($el.height() > 64) {
                    $el.text(addEllipses);
                    var txt = 'Fair consideration/no fraudulent conveyance';
                    if(text.indexOf(txt) !== -1 ){
                        $el.text(txt + '...');
                    }
                }
            }           
        });
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.