Witajcie,
mam pewien problem a mianowicie jak zrobić sumowanie linijek tekstu w textarea za pomocą JS (chce by to dynamicznie było sumowane od razu po wprowadzeniu znaku czy wstawieniu entera. Skrypt powinien sumować z pominięciem linijek gdzie są same spacje oraz linijek gdzie jest enter. Czyli jak klikniemy enter i nie wprowadzimy nic do linii to ta linia nie powinna być sumowana.
Z góry dziękuję za pomoc
0
0
Może pomoże:
http://stackoverflow.com/questions/18581113/count-of-non-blank-lines-in-a-text-file-with-javascript
Tylko, ze liczysz i wypisuje w zdarzeniu http://www.w3schools.com/jsref/event_onkeypress.asp
0
<html>
<head>
<script>
function countLines(event, textInputId, lineCountId) {
var str = document.getElementById(textInputId).value;
var lineCount = document.getElementById(lineCountId)
for(var i=0; i<lineCount.childNodes.length; i++) {
lineCount.removeChild(lineCount.childNodes[i]);
}
var count = 0;
var lines = str.split("\n");
for(var line in lines) {
if (lines[line].trim().length > 0) {
count++;
}
}
lineCount.appendChild(document.createTextNode(count));
}
function init() {
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
}
</script>
</head>
<body onload="init()">
<div>
<textarea rows="10" cols="50" name="textInput" id="textInput" onkeyup="countLines(event, 'textInput', 'lineCount')" ></textarea>
</div>
<div name="lineCount" id="lineCount">
</div>
</body>
</html>