Reputation: 33
I have multiple divs generated by PHP and I am attempting to limit characters on each of the 'div p' tags I have that are under this class.
Each of the paragraphs have a different quote, but need to be limited to a word count of 140. However I'm sure I've overlooked some part to make it select each 'div p' one by one and adjust with the character limit.
var txtlimit = $('.release p').text();
txtlimit = txtlimit.substring(0, 141);
$('.release p').html(txtlimit);
console.log(txtlimit);
Upvotes: 0
Views: 111
Reputation: 1201
The way you're selecting the p
tags you would set them all with the same text as the first, you need to pass through each
of them to set their texts accordingly
$.each($('.release p'),function(index, value){
var txtlimit = $(this).text().split(" "); //splits into array of words
$(this).html(txtlimit.slice(0,139).join(" ")); //get words from 0 to 139
});
FIDDLE: http://jsfiddle.net/y53r7/3/
Upvotes: 0
Reputation: 194
You should do this from your php script. but if you want to do thist at client side for somewhat reason you can do this as..
$('.release p').each(function(){
var txtlimit = $trim($(this).text());
if (txtlimit.length > 140) {
txtlimit = txtlimit.substring(0, 140);
$(this).html(txtlimit);
}
});
## sorry i have missed your lines for word count upto 140
$('.release p').each(function(){
var aParagraphTxt = $trim($(this).text()).split(" ");
var wordCount = aParagraphTxt.length;
if (wordCount > 140) {
$(this).html(aParagraphTxt.slice(0,139).join(" "));
}
});
Upvotes: 1
Reputation: 3705
Did you mean to do this?
$('.release p').each(function() {
var txtlimit = $(this).text();
txtlimit = txtlimit.substring(0, 141);
$(this).html(txtlimit);
console.log(txtlimit);
}
Upvotes: 0