How to put the focus on a certain input field on a modal form using Bootstrap? #40855
-
I've put my first steps in Bootstrap in combination with Flask and all works well so far. I'm using Bootstrap version 4.6.2. I have an app running with the following, simple layout: When I click on the "Add New Wiring Connection" I have a modal form popping up like so: Again, all is working fine. However, if the modal window opens I would like to have the first input field (input field belonging to "Device name:") to automatically have the focus and its surrounding border blueish (standard border colour when having the focus). It surprises me that Bootstrap, with such a huge and overwhelming amount of features and/or possibilities, doesn't have this at hand by default. I read here that, because of HTML5, the I tried this solution but it doesn't work. Most probably - if not surely - because of my still not huge experience with this framework. Below is the code I have that creates the modal form:
The item of interest is the Also note that I've put Later in the code (just before the closing
If I use the Chrome debugger (pressing F12) and put a breakpoint on the variables then I see the variables are getting the correct element assigned by the I've also put a breakpoint on the line that should set the focus: When I open the modal form then that breakpoint is hit (I also see the log text on the Chrome debugger console). But the first input field itself is still not having the focus (the cursor is not put automatically in that input field nor is the border of the input field blueish). Result: my first input field on the modal form is not getting the focus I want it to have and the cursor is not put automatically in that first input field so that the user can start typing right away without having to select that first input field first. Can somebody point me to the problem in the code? Best, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hello @GeVanCo |
Beta Was this translation helpful? Give feedback.
-
Hi Julien, My oh my! I think I mixed up Bootstrap 4 and Bootstrap 5 stuff here. You're absolutely correct. When following your guidelines all is working as I expect. Thanks very much for pointing this out. And thanks also for letting me know CodePen. Never heard of it before but very useful! Best, |
Beta Was this translation helpful? Give feedback.
Hello @GeVanCo
I tried the code you provided in this CodePen, and it works pretty well on Firefox and Chrome. The only thing I changed is that in your
<script>
you try to reachmyFocusedInput
, and it is actuallyfocusedInput
.