jquery - Wrong input being focused -


i have html file bootstrap modal template; inside got 2 inputs inside 2 separated div's difference being: first (input) has countdown maxlength="140" , second doesn't.

i've created function focus input everytime modal open:

// focus input every time modal open $(document).ready(function() {   $('.modal').on("shown.bs.modal", function() {      $('.form-control').focus(); }); 

the problem is: input being focused second (bottom) instead of first.

the issue because have multiple elements same .form-control class. jquery loops on elements finds , triggers focus event on them. result, last element found left focus.

to fix can use :first generically select first input within modal , trigger focus on that:

$('.modal').on("shown.bs.modal", function() {   $(this).find('.form-control:first').focus(); }); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -