I'm trying to count the number of characters entered into 4 text input fields and print the character count into their corresponding number fields like so:
- Character count text input1 -> Number field 1
- Character count text input2 -> Number field 2
- Character count text input3 -> Number field 3
- Character count text input4 -> Number field 4
I found this code snippet in another answer, but its just working for a single input field: Count length of one input field and print it in another with Gravity Forms and Javascript:
<form>
<input name="input_10" id="input_1_10" type="text" value="" class="small" tabindex="1">
<input name="input_2" id="input_1_2" type="text" value="" class="medium" tabindex="2">
</form>
<script>
var bank = document.getElementById("input_1_10");
var countNum = document.getElementById("input_1_2");
bank.addEventListener("keyup", function(e) {
countNum.value = bank.value.length;
});
</script>
As I'm a total newbie to javascript and jQuery, I don't know how to modify this snippet to get it working. Any ideas or suggestions?
Thanks for your help!
You could repeat repeat
addEventListener
for all inputs, or you could do it like thisNotice, this code depends on the html markup, so it worn't work if you have anything else between two inputs,
nextElementSibling
selects the next element from current node. nextElementSibling on mdn