0

I have a form in which I want to set focus on the input field. here is the html of form:

<form method="GET" name="searchFormArticles" novalidate="" data-search-form="articles" data-search-form-articles="" action="/rechtstipps" id="searchFormArticles" data-read-only="1">
  <input type="text" name="what" id="searchArticlesWhat" placeholder="Thema / Autor / Titel" class="form-control anw-form-control border-left-0 anw-md-rounded-right-none anw-md-border-right-0 shadow-none form-control" value="" autocomplete="off">

<button name="searchArticles" type="submit" class="btn btn-block btn-primary mt-3 mt-md-0 shadow-none text-nowrap py-md-0 px-9 anw-md-rounded-left-none">
  <i class="far fa-search d-none d-md-inline-block text-primary" aria-hidden="true"></i>
  <span>Suchen</span>
</button>

input field has a name=what attribute and a id attribute as well, how can I catch this input element in vue

1

1 Answer 1

5

You can give autofocus property in the first input field.

<input 
    type="text" 
    name="what" 
    id="searchArticlesWhat" 
    placeholder="Thema / Autor / Titel" 
    class="form-control ..." 
    value="" 
    autocomplete="off" 
    autofocus
>
Sign up to request clarification or add additional context in comments.

7 Comments

I cant add that, I have to jump the focus to the input field after a particular event
Then you can try document.getElementById("searchArticlesWhat").focus();
have a look at this chunk of code : formsWhat.forEach(form => { if (form.getAttribute('data-search-form') === currentSearchType) { form.reset(); /-----here I want to set focus to input field-----/ } }); after reseting the form I want to set focus the input field inside the form
After form.reset(); you can get Element by id and set focus on it. using document.getElementById("searchArticlesWhat").focus();
you mean form.elements[0].focus(), Thank you so much Huzaifa bro, it worked like a charm, Shukriya :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.