benhowdle89
benhowdle89

Reputation: 37504

jquery limit characters

var encoded = $("#bauer").text();
var charLength = encoded.length;

I then want to take encoded and put it in a tweet. How can i make sure the charLength doesnt exceed 140 characters for the tweet - ie. it still sends the tweet but trims it below 140 characters?

Upvotes: 1

Views: 4946

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1075755

I don't think I understand the question. Because if I do, add:

encoded = encoded.substring(0, 140);

...after which the encoded string will be (at most) 140 characters long. (You don't even need to check charLength first, if you don't mind possibly making one unnecessary function call; substring doesn't care if the second parameter is out of bounds.)

Upvotes: 0

darioo
darioo

Reputation: 47213

Use encoded.substring(0,140) for this task (if you really want trimming to occur).

Upvotes: 0

brian-d
brian-d

Reputation: 803

You could use javascript's substring method.

if(encoded.length > 140){
 encoded = encoded.substring(0, 140);
}

Upvotes: 3

Related Questions