Well, I have a Javascript function that returns the number of words that I write into a textarea, but for some reasons, I wanted to write it using Jquery. And, although I tried, I didn't managed to get it, so that's why I'm here, I would be very, very grateful if you could help me.
This is the Javascript:
function contar() {
palabras = 1;
if (document.data.texto.value.length === '') palabras = 0;
for (count = 0; count < document.data.texto.value.length; count++) {
if (document.data.texto.value.substring(count, count + 1) == ' ') palabras++;
}
document.data.cuenta.value = palabras;}
And this is the jQuery I've made:
(function () {
var palabras = 1;
if ($("[name=data] textarea[name=texto]").val().length === "") {
palabras = 0;
}
for (i = 0; i < $("[name=data] textarea[name=texto]").val().length; i++) {
if ($("[name=data] textarea[name=texto]").val().substring(i, i + 1) == ' ')) {
palabras++;
}
}
});
Finally, this is the HTML I use to display it:
<form name="data">
<textarea name="texto" cols="40" rows="4" onkeyup="contar()" onkeypress="contar()"></textarea>
<br>
<input type="Text" name="cuenta" size="3" maxlength="3">
</form>