I want something like a counter for every linebreaks in a textarea. This is used when uploading a huge number of webbanners, so i need a simple counter for how many a user has added. Right now the counter works only when a user presses a link with a special bannersize.
This is my code as it is right now:
var i = 0;
$('.size').click(function(){
$('#total-number').text(i++);
return false;
});
Utilize the String split
function...
var text = $('#total-number').text();
var eachLine = text.split('\n');
alert('Lines found: ' + eachLine.length);
for(var i = 0, l = eachLine.length; i < l; i++) {
alert('Line ' + (i+1) + ': ' + eachLine[i]);
}
Using regular expressions, your problem can be solved like this (searching the textarea for the new line character "\n")):
//assuming $('#total-number') is your textarea element
var text = $('#total-number').val();
// look for any "\n" occurences
var matches = text.match(/\n/g);
// count them, if there are any
var breaks = matches ? matches.length : 0;
breaks
variable now contains number of line breaks in the text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With