# onStartup self.focus()

**URL:** <https://forum.inductiveautomation.com/t/onstartup-self-focus/91546>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [June 7, 2024, 2:10pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546 "2024-06-07T14:10:55Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [June 7, 2024, 2:10pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/1 "2024-06-07T14:10:55Z")

</div>

I have a text field in a popup view, and I added an onStartup event script that is simply self.focus(). While the script seems to execute every time the popup is opened, the focus only seems to stick about 50% of the time. I'm guessing I could use a Timer to delay the focus a tad and make it stick, but I'd rather not use such a hack if possible. Am I missing something?

EDIT: Yep, this works:

```python
def runAction(self):
	from threading import Timer
	
	def focus(self):
		self.focus()
		
	Timer(0.1, focus, [self]).start()

```

...but why is that necessary?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 7, 2024, 2:16pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/2 "2024-06-07T14:16:27Z")

</div>

What are you trying to focus, the popup itself?

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [June 7, 2024, 2:19pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/3 "2024-06-07T14:19:23Z")

</div>

The text input

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 7, 2024, 2:20pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/4 "2024-06-07T14:20:51Z")

</div>

I didn't read your post close enough. I do this as well and it works for me. `onStartUp` event on the view

```python
self.getChild("root").getChild("TextField").focus()

```

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [June 7, 2024, 2:25pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/5 "2024-06-07T14:25:18Z")

</div>

When a Popup is opened, what would you expect to receive focus? We can't just arbitrarily apply focus to the first component (input) in the Popup, so focus is (I believe) applied to the Popup itself.

If you want to determine where focus should go, you need to do something like this.

> [@dkhayes117](#):
>
> I do this as well and it works for me.

That very likely only works for a very simple View. As the View becomes more complicated, immediate accessing of internal components during onStartup will become increasingly riskier as it becomes something approaching a race condition. It's _probably_ a bit safer to just do `self.focus()` in the `onStartup` Event of the Text Field component.

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [June 7, 2024, 2:29pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/6 "2024-06-07T14:29:57Z")

</div>

May be safer, but it doesn't work, that's the point.

I tried doing it from the onStartup of the view, but I get the same results. It only works about 50% of the time.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 7, 2024, 2:31pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/7 "2024-06-07T14:31:48Z")

</div>

What about start up script on the container the component is in?

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [June 7, 2024, 2:32pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/8 "2024-06-07T14:32:12Z")

</div>

Like I said: race condition. The `onStartup` Event fires _as the View begins to load_, and there's a chance that the content of the View hasn't "loaded" quite yet. Even within the context of the component, it may not be in a state where it can even receive focus yet.

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [June 7, 2024, 2:34pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/9 "2024-06-07T14:34:19Z")

</div>

Makes sense. An onLoad alternative that fires when the view is done loading would be ideal...

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 7, 2024, 2:36pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/10 "2024-06-07T14:36:56Z")

</div>

The view I use this on is a single container, basically a confirmation popup. It works every time.

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [June 7, 2024, 2:41pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/11 "2024-06-07T14:41:14Z")

</div>

That's exactly what I'm creating:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/5/252031ee4fc15b0190dd7d41c31f2639a1a509f4.png)  
Very simple, and it doesn't seem like it should be so convoluted to simply make sure the input field has focus when it opens...

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 7, 2024, 2:44pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/12 "2024-06-07T14:44:36Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/f/0f4db3f6deb77135d8ad44a5d452990414e7543b.png)

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [June 7, 2024, 2:46pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/13 "2024-06-07T14:46:55Z")

</div>

I tried that, same result...

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [June 7, 2024, 3:06pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/14 "2024-06-07T15:06:13Z")

</div>

_In theory_, if you have a component which you know will load _after_ your Text Field, you could move the logic to that component.

```python
# onStartup Event of "later" sibling component
self.getSibling("TextField").focus()

```

It would disassociate the logic from the parent and Text Field, but it should work.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 7, 2024, 5:27pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/15 "2024-06-07T17:27:51Z")

</div>

> [@cmallonee](#):
>
> _In theory_, if you have a component which you know will load _after_ your Text Field

How would you determine what would load after it?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 10, 2024, 7:38am UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/16 "2024-06-10T07:38:28Z")

</div>

Could a "onComponentFinishedLoading" hook be possible, and would you consider adding it ?  
It seems like a lot of these issues would be solved by having a different event that's triggered "later".

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [June 10, 2024, 4:19pm UTC](https://forum.inductiveautomation.com/t/onstartup-self-focus/91546/17 "2024-06-10T16:19:31Z")

</div>

I created a feature request, vote for it here:

> **[Perspective view onLoad event that fires when the view is done loading |...](https://inductiveautomation.canny.io/ignition-features-and-ideas/p/perspective-view-onload-event-that-fires-when-the-view-is-done-loading)**
>
> This would be the only clean way of ensuring that, for example, an input .focus() would execute at the right time. https://forum.inductiveautomation.
