JQuery.next (input OR textarea)

I was messing around with this simple line of code:

$(this).next('input').focus(); 

I would like for him to choose the following (input OR textarea) , whichever comes first - how can I do this?

Alternatively, how can I make it just focus on the next element in tabindex ?

Edit

Since the next input / tabindex is not always right after $(this) , how can I easily go through the list of siblings until I find the next match?

+6
source share
2 answers

Use :input , which selects all input elements, textarea, select, and button.

 $(this).next(':input').focus(); 

jsFiddle

If the next input is not a sibling, this seems to work ... In this example, click the text field with id="test" and it will focus in the following element :input .

 <input type="text" value="hello world2"/> <input type="text" value="hello world3"/> <div> <input type="text" id="test"/> </div> <div> <div>Hi</div> </div> <textarea>found me</textarea> <input type="text" value="hello world"/> $(document).ready(function(){ $('#test').click(function(){ var c = $(this).parents().nextAll(':input:first').focus(); }); }); 

jsFiddle2

+7
source
 $(this).next('input,textarea').focus(); 

But it will only work if the next input / textarea is a sibling right after this .

Alternatively, you can use the :focusable selector from the jQuery user interface.

Script Example - Press Enter to Focus Next Input

You can also configure the first entry if there are currently no inputs tied to the document itself and using e.target .

+6
source

Source: https://habr.com/ru/post/898899/


All Articles