0

How can I find out which of the input fields was clicked or is currently selected. I use 13 input fields. These should be checked whether the input that was specified fits. Is there a possibility to interact with the fields or is it better to give each input field an id and then read it out and compare it? attached the test html code

        <div class="d5246b-task js-d5246b-inputs">
          <div class="d5246b-find">
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
            <p class="d524b-input-fix-letter">b</p>
            <input class="d5246b-input-letter" placeholder="_">
          </div> 
          <div class="d5246b-find">
            <p class="d524b-input-fix-letter">l</p>
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
            <p class="d524b-input-fix-letter">n</p>
          </div> 
          <div class="d5246b-find">
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
            <p class="d524b-input-fix-letter">r</p>
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
          </div> 
          <div class="d5246b-find">
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
            <p class="d524b-input-fix-letter">y</p>
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
          </div> 
          <div class="d5246b-find">
            <p class="d524b-input-fix-letter">e</p>
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
            <p class="d524b-input-fix-letter">g</p>
          </div> 
          <div class="d5246b-find">
            <p class="d524b-input-fix-letter">o</p>
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
            <p class="d524b-input-fix-letter">q</p>
          </div> 
          <div class="d5246b-find">
            <p class="d524b-input-fix-letter">c</p>
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
            <p class="d524b-input-fix-letter">e</p>
          </div> 
          <div class="d5246b-find">
            <p class="d524b-input-fix-letter">t</p>
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
            <p class="d524b-input-fix-letter">w</p>
          </div> 
          <div class="d5246b-find">
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
            <p class="d524b-input-fix-letter">h</p>
            <input class="d5246b-input-letter" max-lenghth="1" placeholder="_">
          </div> 
        </div>

Last information: sorry that I did not consider this and did not provide the information. the idea is that only one input is allowed. so an "a" as an example. if the input fits. the input should jump further.

stillday
  • 93
  • 2
  • 10
  • Every jQuery event has `this` that points to current element – Justinas Aug 25 '20 at 11:20
  • 2
    or you can check the `event.curentTarget` – Pete Aug 25 '20 at 11:22
  • @Justinas, if event callback is written with `arrow` function then `this` will not work. I agree with @ Pete to use `event.currentTarget`. – Karan Aug 25 '20 at 11:24
  • "*or is currently selected*" - https://stackoverflow.com/q/967096/2181514 `$(":focus")` – freedomn-m Aug 25 '20 at 11:24
  • 1
    We really need to know your scenario better to advise how to determine "*whether the input that was specified fits*". Also note that "is it better" is off topic for SO. – freedomn-m Aug 25 '20 at 11:25
  • @freedomn-m Right. sorry that I did not consider this and did not provide the information. the idea is that only one input is allowed. so an "a" as an example. if the input fits. the input should jump further. – stillday Aug 25 '20 at 11:28

1 Answers1

1

Add an id in each input and in your JS code put this

$('.d5246b-input-letter').click(function (e) {
var clicked = $(this).attr("id");
});

  • 4
    Hi, welcome to SO. Note that the question asks to find the element, not find its `id`. Adding an id is not required in this scenario (to find which element was clicked) (it may be required for OPs overall scenario, which hasn't been explained in the question). – freedomn-m Aug 25 '20 at 11:22
  • is an example of how you could use – Rodrigo Vergara Aug 25 '20 at 11:29