Javascript:
//Simplifiquei o JS, baseado na versão do @vmartins, muito mais enxuta
document.getElementById('texto').onkeyup = function() {
count = this.value.split("\n").length;
document.getElementById('contador').innerHTML = count;
document.getElementById('linhas').value = count;
}
HTML:
<form action="" method="post">
<textarea id="texto" name="texto" onKeyup="updateLineCount()" rows="10"></textarea><br>
<input type="hidden" id="linhas" name="linhas" value="0">
Linhas: <span id="contador"></span>
</form>
See demonstration on SQL Fiddle
Note: the line count will be sent in a field hidden, to answer the question, however the ideal is to reconnect via PHP, and not send the value, as this can be modified manually and/ or simulated.
PHP:
(we are not using the HTML hidden field, but "reconnecting" via PHP)
<?php
$texto = $_POST('texto');
$linhas = explode("\n", $texto);
$conta = count($linhas);
//... se for mostrar as linhas individualmente,
// pode iterar a array $linhas aqui ...
echo $conta;
?>
I don’t quite understand your question, but do you want to create a line break counter? but count lines than? can you give an example? Obs:I cannot comment on your question as I do not have enough reputation for it.
– Rech
@Jahnkrauss I need to create a texte area and while I type I need a function that records the number of lines (each line I type) and that records this value in a variable so I can automatically print it with Javascript, as if it were a line counter
– user7162
@user7162 Would that be something?
function countLines(area)
{
var text = area.value.replace(/\s+$/g,"")
var split = text.split("\n")
return split.length
}
Sorry for formatting but I don’t know why I’m not able to format the text after just sending the result to a php variable.– Rech
I added an answer, but I’m not sure what you want. If you want to display line-by-line numbering, specify better in the question.
– Bacco