0

I'm trying to make a function that ellipsis the text, given a number of max letters.

I got an array with all the text of the classes, already formated the way i want. The problem is that i need to change the text() of every class with the text that is in the array.

Here is my code:

var array = $('.elipse').map(function(){
    return $(this).text();
}).get();


var i;
var teste = [];

for (i=0;i<array.length;i++){
    if (array[i].length > 30){

    teste.push(array[i].substr(0,10));

    } else {
        teste.push(array[i]);
    }
}

for (var i=0;i<teste.length;i++){

     $('.elipse').each(function(){

      $(this).text(teste[i]);

    });

  } 

The problem is in the last for loop. Every text of every element that contains the elipse class must be changed to the text in teste array. I tried to loop it in a lot of different ways, but im missing something

1
  • each elipse text will always be the last item in the teste array, because every time the for loop gets in, it starts interating the each function again from start Commented Mar 31, 2014 at 13:48

1 Answer 1

2

Just loop the elements only, else you're overwriting:

$('.elipse').each(function(i){

  $(this).text(teste[i]);

});
Sign up to request clarification or add additional context in comments.

1 Comment

THANKS, didnt know how to use the each function properly.. just missing the parameter :))

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.