# Put focus back on Inputs.Select after pressing button

**URL:** <https://talk.observablehq.com/t/put-focus-back-on-inputs-select-after-pressing-button/9587>\
**Category:** Help\
**Created:** [July 31, 2024, 5:50pm UTC](https://talk.observablehq.com/t/put-focus-back-on-inputs-select-after-pressing-button/9587 "2024-07-31T17:50:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dampfnudel](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@dampfnudel](https://talk.observablehq.com/u/dampfnudel)\
**Post date:** [July 31, 2024, 5:50pm UTC](https://talk.observablehq.com/t/put-focus-back-on-inputs-select-after-pressing-button/9587/1 "2024-07-31T17:50:19Z")

</div>

Hi,

I’m using a wrapper function to submit a form, thanks to @mootari, see [Submittable Input Forms / Fabian Iwand | Observable](https://observablehq.com/@mootari/inputs-submit)

I have two Inputs.Select lists: the first one you select a Category then the second list shows a set of Items for that specific Category. So each time you change Category, the Item list also changes. Below the two lists I have an Inputs.Form with a text box and Submit button.

What I need to do is move the focus back to the “Select Item” list when some text is entered and the Submit button is pressed. I can make it work with document.getElementById(‘’).focus() in the function that runs when the Submit button is pressed.

The problem is that the Id changes when I change the Select Category list, which is the one that controls the Select Item list. How can I focus on something when the Id keeps changing?

See my notebook to reproduce: [Focus Back on Select Input / Obs Data App | Observable](https://observablehq.com/d/31e429c914fa9ac6)

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [July 31, 2024, 6:40pm UTC](https://talk.observablehq.com/t/put-focus-back-on-inputs-select-after-pressing-button/9587/2 "2024-07-31T18:40:41Z")

</div>

You can use a side effect by adding a cell like

```js
values, viewof selectItem.input.focus()

```

This cell will rerun whenever `values` changes and then focus the select element.

As an aside, you should not need to use getElementById() in notebooks since you can usually reference elements directly. Another benefit of doing it this way is that you declare an explicit dependency on the referenced element which Observable’s notebook runtime can use to update dependent cells reactively.

---

<div class="post-metadata">

**Author:** ![dampfnudel](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@dampfnudel](https://talk.observablehq.com/u/dampfnudel)\
**Post date:** [July 31, 2024, 7:00pm UTC](https://talk.observablehq.com/t/put-focus-back-on-inputs-select-after-pressing-button/9587/3 "2024-07-31T19:00:16Z")

</div>

Thank you for the fast response. Perfect solution. I still have a lot to learn 😀

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [July 31, 2024, 8:02pm UTC](https://talk.observablehq.com/t/put-focus-back-on-inputs-select-after-pressing-button/9587/4 "2024-07-31T20:02:17Z")

</div>

The `viewof` prefix might seem a bit magical, but at its core it’s just a macro that causes Observable to produce two _variables_ from the cell:

- the DOM element that holds the input, which you can still reference by adding `viewof ` before the name
- an async generator of the element’s value that yields new values every time an input event happens.

If you want to dive a little deeper into the topic you might find this short guide helpful:

> **[What has viewof ever done for us?!](https://observablehq.com/@mootari/what-has-viewof-ever-done-for-us)**
>
> A short introduction to \`viewof\`, in response to this post. \`viewof\` is an Observable macro (i.e., a "magic word") that allows you to create user interfaces for your values. Observable cells by themselves are rather boring: they can only store...
