Enabling and focusing a TextFeild on an event in Perspective

I have a Disabled TextField component which should be enabled and on focus when I click on a button. The TextField becomes enabled but focus does not change on the first click.
This is the script I have made:

self.getSibling("TextField").props.enabled=True
self.getSibling("TextField").focus()

Is there any way to achieve this functionality on single click?

Where is that code executed?

Tip: use the </> code formatting button, not the > quotation button.

1 Like

It’s run on ‘onclick’ event script of a button that I made near the TextField

I’d say there’s a delay on the view learning that the component is enabled and that the focus() command is fired before it’s ready.

	from time import sleep
	
	self.getSibling("TextField").props.enabled = True
	sleep(0.1)
	self.getSibling("TextField").focus()

works in Designer. I didn’t try it in a browser so you might need to test and increase the delay slightly.

This variation has the advantage of exiting as soon as possible and may feel a little more responsive but at the expense of five lines of code rather than the single sleep() instruction. A Python expert may be able to compress it for you.

	self.getSibling("TextField").props.enabled = True
	i = 0
	while i < 100:
		i += 1
		if self.getSibling("TextField").props.enabled == True:
			break
	self.getSibling("TextField").focus()

This worked on browser :grinning:
Thanks for that

I believe something like this would work:

any(self.getSibling("TextField").props.enabled for _ in xrange(timeout))

This would iterate until either timeout is reached or [...].enabled returns True

1 Like