I need to do a jQuery script to mirror what I type in an <input>
of class="a"
to the next forward <input>
of class="b"
, for every key pressed in the keyboard.
But I have some restrictions:
No, unfortunately I can't change the HTML. It is a legacy code. The only thing I can do is add a jQuery script.
No, it can't be a pure JavaScript code. It must be a jQuery code.
Yes, I need to use
.on()
because thediv
that contains the inputs are created dynamically.
I have tried using .next()
, .find()
, .each()
, .closest()
... but I'm a beginner, so I don't know how to use them properly. Probably it is something using these functions.
<!-- any code -->
<div>
<input type="text" class="a" />
</div>
<!-- any code -->
<div>
<input type="text" class="b" />
</div>
<!-- any code -->
<div>
<input type="text" class="a" />
</div>
<!-- any code -->
<div>
<input type="text" class="b" />
</div>
<!-- any code -->
<div>
<input type="text" class="a" />
</div>
<!-- any code -->
<div>
<input type="text" class="b" />
</div>
<!-- any code -->
$(document).on('keyup', '.a', function (ev) {
//1 - get value in input and store in a variable
//2 - search for the next forward input of class="b"
//3 - set the variable to the value of input
});
IMPORTANT: Please note that the code isn't exactly like this. Between one input
and the other, there is some code, and they aren't event in the same parent div
. The only thing I cant assure is that the next input
is after, in the code, ant that it'll be inside a div
.