0
$('document').ready(function(){
    //textoverflow($('.content'),100);

    $('span').click(function(){
        //disable textoverflow function & output full text
    });
});

function textoverflow(ele, num){
    ele.each(function() {
        $(this).text(
            $(this).text().slice(0,num) + '...'
        ).append('<span>More</span>');
    });
}

I have a text use a function slice the content.

There is a button when user click, i want to disable function only for $this .content & output all text without slice.

How to disable function?

5
  • 2
    No where do you keep a copy of the orginal, so how would you be able to undo it? Modern day CSS has text-overflow: ellipsis Commented Feb 7, 2014 at 6:52
  • 1
    text overflow only works for single line Commented Feb 7, 2014 at 6:53
  • 1
    But you do need to keep a copy of the original to be able to restore it at a later point. Commented Feb 7, 2014 at 6:54
  • ya its better to keep original Commented Feb 7, 2014 at 6:54
  • So you loop through and add the original text back... Commented Feb 7, 2014 at 6:55

3 Answers 3

1

Make a copy of the orginal and store it in a data attribute. Loop through and put the text back.

function textoverflow(ele, num){
    ele.each(function() {
        var item = $(this);
        var orgText = item.text();
        if (orgText.length>num) {
            item.data("orgtext", orgText);
            item.text( item.text().slice(0,num) + '...');
            item.append('<span>More</span>');
        }
    });
}

function undoTextoverflow(ele){
    ele.each(function() {
        var item = $(this);
        var orgText = item.data("orgtext");
        if (orgText) {
            item.text(orgText);
        }
    });
}


$(function(){
    textoverflow($('.content'),100);

    $('.content').on("click", "span", function(){
        var parentElem = $(this).closest(".content");
        undoTextoverflow(parentElem);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

is that possible use span $(this) and only open the text, since i have may text have same class and share this function
I showed you how to call it with the click of the span. Ideally it would be an anchor.
1
$('document').ready(function(){

    function bindTriggerOn($ele, num, yourEvent) {
        var dot = '...', more = '<span>More</span>';
        $ele.on(yourEvent,
            $ele.each(function() {
                var $me = $(this);
                $me.text($me.text().slice(0, num) + dot).append(more);
            });
        });
        $ele.trigger(yourEvent);
    };
    function offOn($ele, yourEvent) {
        $ele.off(yourEvent)
    };
    function textoverflow($ele, num, bl){
        var myEvent = "myEvent";
        if(bl) {
            bindTriggerOn($ele, num, myEvent);
        } else {
            offOn($ele, myEvent);
        }
    };
    var $content = $('.content');
    textoverflow($content, 100);

    $('span').click(function(){
        textoverflow($content, 0, false);
    });
});

Comments

1

You need to keep a copy of the original to be able to restore it later. For example:

function truncate(ele, num){
    ele.each(function() {
        var self = $(this); // cache this, since we are using it in 3 places
        // keep a copy of the original text, before truncating
        self.data('original', self.text()).text(
            self.text().slice(0,num) + '...'
        ).append('<span>More</span>');
    });
}

function showFull(ele){
    ele.each(function() {
        var self = $(this);
        // restore orignial and remove the More link
        self.text(self.data('original').find('span').remove();
    });
}

$('document').ready(function(){
    truncate($('.content'),100);

    $('span').click(function(){
        showFull($(this).closest('.content'));
    });
});

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.