Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Focus multiple inputs at once

How can I focus more than one input when I get validation errors from the controller? I'm using Inertia and Vue Tailwind, this is my code. The inputs are never all put into focus, what am I doing wrong?

<t-input-group  
	:variant="{danger: $page.props.errors.name}"   :feedback="$page.props.errors && $page.props.errors.name ? $page.props.errors.name : ''">

	<template slot="label">
		<strong class="text-red-600">* </strong>
		Name
	</template>

	<t-input ref="name" v-model="memo.name" placeholder="Name"
		:variant="{danger: $page.props.errors.name}"/>
 </t-input-group>


//method
createOrUpdateMemo() {
            if (this.memo.id) {
                this.memo.put(`/memos/memo/${this.memo.id}`);
            } else {
                this.memo.post(`/memos/memo`, {
                    onError: errors => {
                        if (errors.name) {
                            this.$refs.name.$el.focus();
                        }
                        if (errors.ac) {
                            this.$refs.ac.$el.focus();
                        }
                    },
                });
            }
        },

I was trying to use focus because the inputs already have a error mode :variant="{danger: $page.props.errors.name}" this sets the borders and focus to red, so I would like to see that when there is an error.

Right now only the second input is being focused, I would like to focus more that one at a time. My idea really is just to focus the inputs that have errors, but if using focus isn't correct what can I do to make these inputs standout a bit?

0 likes
3 replies
Tray2's avatar

Only one element can be focused at the time, So focus on the first validation error and then the next one when you have corrected the validation error.

1 like
drehimself's avatar

You can only focus one element at a time.

What you can do is focus the first element with errors, but highlight the rest of the form elements with some sort of visual indicator. Maybe a red border or actual red text underneath each form element with an error.

1 like
eggplantSword's avatar

@drehimself I already have the error messages below the inputs when they have errors. I think I'll follow your advice and make it more obvious with a highlight on the input itself instead of a focus.

Please or to participate in this conversation.