0

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 the div 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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pablo Roxo
  • 13
  • 3

2 Answers2

0

[1] To refer to the input you typed in you need to use $(this)

[2] To get its value use .val()

[3] To select the next .b element you need to use .next('.b')

$(this).next('.b').val($(this).val()) and I prefer to use input event instead of keyup

$(document).on('input', '.a', function (ev) {
    //1 - get value in input and store in a variable
    var GetThisValue = $(this).val();
    //2 - search for the next forward input of class="b"
    //3 - set the variable to the value of input
    $(this).next('.b').val(GetThisValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
    <input type="text" class="a" />
    <input type="text" class="b" />
</div>
<div>
    <input type="text" class="a" />
    <input type="text" class="b" />
</div>
<div>
    <input type="text" class="a" />
    <input type="text" class="b" />
</div>

[Other Selectors to select .b]

  • $(this).parent().find('.b')

  • $(this).closest('div').find('.b')

Snippet up to OP update

$(document).on('input', '.a', function (ev) {
    //1 - get value in input and store in a variable
    var GetThisValue = $(this).val();
    //2 - search for the next forward input of class="b"
    //3 - set the variable to the value of input
    $(this).parent().nextAll('div:has(.b):first').find('.b').val(GetThisValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
    <input type="text" class="a" />
</div>
<div> any code </div>
<div>
    <input type="text" class="b" />
</div>
<!-- any code -->
<div>
    <input type="text" class="a" />
</div>
<div> any code </div>
<div>
    <input type="text" class="b" />
</div>
<div> any code </div>
<div>
    <input type="text" class="a" />
</div>
<!-- any code -->
<div>
    <input type="text" class="b" />
</div>
<!-- any code -->
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • Mohamed, your code works! But I made some changes in the code of my question. I added an "IMPORTANT" note in the end. I forgot to mention that the next class="b" input can be in the entire code. Can you please continue helping? Thank you very much! – Pablo Roxo May 26 '19 at 02:03
  • @PabloRoxo The **Selector** is a sensitive thing depending on the HTML structure even it was a css .. So without the exact HTML structure you have no one can guess the selector you need .. I added other selectors in my answer may help – Mohamed-Yousef May 26 '19 at 02:06
  • Can you wait until Monday for me to get the exact code? I'll update the question and comment here, so you will be notified. And I'll continue trying and seaching for solutions. But on monday I'll have the exact code. Many thanks! :) – Pablo Roxo May 26 '19 at 02:09
  • You're totally welcome anytime @PabloRoxo .. Have a great day :-) – Mohamed-Yousef May 26 '19 at 02:10
  • I found a solution for my actual code in question (after edit) doing something like this: ```$(this).parent().next('div').children('.b').val(data);``` (whrere ```data``` has ```$(this).val()```. Although I didn't like my solution at all, it works. And I've learned about the hierarchy of tags & handling them with ```parent()```, ```next()``` and ```children()``` functions. It'll work in my code, but I'll keep this as a plan B. I think a code by you will be with best practices. :D – Pablo Roxo May 26 '19 at 02:54
  • I glad that you find a solution @PabloRoxo .. But `$(this).parent().next('div')` will select the next div whatever it contains `.b` class or not .. I updated my answer I uncomment some of `any code` lines and type another selector which will work for the next div which contains `.b` class in it .. Have a great day :-) – Mohamed-Yousef May 26 '19 at 05:06
  • PERFECT!!! Now your updated code does EXACTLY what I need! I made several tests here and and it searches the next ```class="b"``` in any part of the code. It just needs to be inside a ```div```, and I can assure it is. Mohamed, you're wonderful! Thanky you very much! – Pablo Roxo May 26 '19 at 15:51
  • I'm still studying this and I found a ```parents()``` function, in plural. That's something to go higher in the parents tree, I think. Anyway, I already have your solution working! – Pablo Roxo May 26 '19 at 15:58
  • @PabloRoxo I always use [`.closest()`](https://stackoverflow.com/questions/11756529/jquery-find-nearest-matching-element) instead of `parent()/parents()` ..I'm glad you find a solution .. Have a nice day :-) – Mohamed-Yousef May 26 '19 at 17:23
  • Mohamed, I'm here just to tell you that your code worked in my real scenario. Thank you very much! – Pablo Roxo May 27 '19 at 12:02
0

Seeing how jQuery is pure JavaScript, one might argue that this is also "jQuery code" :D

  1. on an input event, get the cousin input by getting the parent element's sibling (aunt element)
  2. if the aunt isn't a <div> keep searching, until finding the next <div>
  3. search under aunt for the desired input/class combo (i.e. input.b)
  4. set value of that cousin element to the value of the input.a that is being typed in

document.addEventListener('input', event => {
  if (event.target.classList.contains('a')) {
    let aunt=event.target.parentElement.nextElementSibling
    while(aunt && aunt.nodeName!='DIV'){
      aunt=aunt.nextElementSibling;
    }
    const cousin = aunt.querySelector('input.b');
    cousin.value = event.target.value;
  }
});
<!-- any code -->
<div>
  <input type="text" class="a" />
</div>
<pre>
  code
</pre>
<div>
  <input type="text" class="b" />
</div>
<pre>
  code
</pre>
<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>
<pre>
  code
</pre>
<div>
  <input type="text" class="b" />
</div>
Mike
  • 1,279
  • 7
  • 18