Validation characters remaining

-1

Create the JavaScript function called "AccountRestores (idText, idSpan)" that counts how many characters remain in the "txtVoce" field and show this amount of characters remaining in span "carResTxtVoce". You set the field in the HTML code to have 40 columns and 10 rows, that is, 400 characters. Tip: The function must be called in the onChange event of the "txtVoce" field. Tip 2: The input arguments idC field and idSpan should be the id's of the "txtVoce" field and the span "carResTxtVoce".

Please have tried, but I can not solve this question, can you help me?

<div><label for="txtVoce">Faleme sobre você:</label></div>
            <div><textarea id="txtVoce" maxlength="100" name="txtVoce" cols="40" rows="10"></textarea>
            <p><span id="carResTxtVoce" style="font-weight: bold;">400</span> caracteres restantes</p>
    
asked by anonymous 14.02.2016 / 01:00

1 answer

2

You can do this:

function contaCaracteresRestantes(idCampoTexto, idSpan) {
  var n = document.getElementById(idCampoTexto).value.length;
  document.getElementById(idSpan).innerHTML = 400 - n;
}
<div><label for="txtVoce">Faleme sobre você:</label></div>
<div><textarea id="txtVoce" maxlength="100" name="txtVoce" cols="40" rows="10" onKeyUp="contaCaracteresRestantes('txtVoce', 'carResTxtVoce')"></textarea>
<p><span id="carResTxtVoce" style="font-weight: bold;">400</span> caracteres restantes</p>

If you want the counter to update as you type (as in the example above), use onKeyUp and / or onKeyDown instead of onChange , which was suggested in the statement. If you use onChange , you will have to click outside the textarea for the counter to update.

    
14.02.2016 / 01:31