# Strange behaviour in onFocusGained

**URL:** <https://forum.servoy.com/t/strange-behaviour-in-onfocusgained/3064>\
**Category:** Classic Servoy\
**Created:** [November 16, 2004, 1:34am UTC](https://forum.servoy.com/t/strange-behaviour-in-onfocusgained/3064 "2004-11-16T01:34:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morley](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Morley](https://forum.servoy.com/u/Morley)\
**Post date:** [November 16, 2004, 1:34am UTC](https://forum.servoy.com/t/strange-behaviour-in-onfocusgained/3064/1 "2004-11-16T01:34:52Z")

</div>

I’ve added the following method to onFocusGained for the field “name”. The purpose is to give the end user an advisory if there is already content in the field. Instead it goes into a perpetual loop, endlessly repeating the message.

```auto
if (name)
{
	var msg = 'Before performing radical editing on this field, please review how this field is currently being used in Company.'
	plugins.dialogs.showWarningDialog('Warning', msg);
}

```

What am I not understanding?

Version R2 2.1.1-build 313  
Java version 1.4.2\_05-b04 (Windows XP)

---

<div class="post-metadata">

**Author:** ![Harjo](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/harjo/32/4873_2.png) [@Harjo](https://forum.servoy.com/u/Harjo)\
**Post date:** [November 16, 2004, 6:47am UTC](https://forum.servoy.com/t/strange-behaviour-in-onfocusgained/3064/2 "2004-11-16T06:47:52Z")

</div>

Give the field a property name: name

Try this:

```auto
if (name) 
{ 
   var msg = 'Before performing radical editing on this field, please review how this field is currently being used in Company.' 
   plugins.dialogs.showWarningDialog('Warning', msg); 
	elements.name.requestFocus(false)
}

```

---

<div class="post-metadata">

**Author:** ![Morley](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Morley](https://forum.servoy.com/u/Morley)\
**Post date:** [November 16, 2004, 1:05pm UTC](https://forum.servoy.com/t/strange-behaviour-in-onfocusgained/3064/3 "2004-11-16T13:05:03Z")

</div>

At first I thought your line of code would take the cursor out of the field and prevent editing. Surprise, it doesn’t. The cursor remains in place! And the loop ends. Thanks Harjo.

I’m puzzled why such a simple function should go into an infinite loop in the first place.

---

<div class="post-metadata">

**Author:** ![Harjo](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/harjo/32/4873_2.png) [@Harjo](https://forum.servoy.com/u/Harjo)\
**Post date:** [November 16, 2004, 1:13pm UTC](https://forum.servoy.com/t/strange-behaviour-in-onfocusgained/3064/4 "2004-11-16T13:13:00Z")

</div>

That is because, you show a popup (the cursor is out the field) than if you close the popup, the cursor is put back in the field, and is firing at the same time the onFocus method again

now with the extra code, de element is requested focus, without firing the onFocus method (that’s the false option)

If you make the false, true again, you’ll end up in a loop again.
