Page 1 of 1

Fire event when textbox changes

Posted: Mon May 08, 2006 5:22 pm
by pickle
Hi all,

I've got a textfield that is being updated with JS via a common calendar library. I'd like to fire an event when the contents of that textfield changes. I can't use onKeyUp() because text isn't being directly typed into the field. I can't use onChange() either because for textfields, onChange() is fired onBlur(). Since I'm never focusing on the field, onBlur() never fires.

Any ideas?

Posted: Mon May 08, 2006 5:50 pm
by feyd
poll the field. :)

setInterval()

Posted: Mon May 08, 2006 6:08 pm
by s.dot
hm, give it temporary focus? onChange="textarea.focus(); this.focus();"

[[ my attempt :) ]]

Posted: Tue May 09, 2006 9:51 am
by pickle
Can't use onChange because that event isn't fired until the field loses focus anyway. I'll probably have to poll the field.

Posted: Tue May 09, 2006 12:36 pm
by Burrito
that seems like a waste of resources...

why don't you fire it when the date is selected from the calendar?

Posted: Tue May 09, 2006 12:42 pm
by pickle
I could but don't want to for 2 reasons. 1) It's a 3rd party library - I'd have to wade through the code to set that up. 2) This may not be the only place I use that library, and I don't want to have it fire the event for other pages.

It is a bit wasteful of resources, but I could start the polling when the calendar is first loaded, and stop polling when the fields are validated.

Right now I'm thinking I just have a 'validate' button beside the dates - seems simplest.

Posted: Tue May 09, 2006 2:06 pm
by Burrito
so the ultimate goal is to validate that the dates are indeed dates? and you're doing that onblur of the field?

if so, just make sure that the dates are formatted correctly from the calendar, another 'easy' fix would be to modify your calendar libarary (very slightly) and make it send focus to the text field where the date is inserted. That should be a piece of cake.

But really, as long as you ensure the dates are formatted correctly from the calendar, then I don't see why you'd need to validate them anyway...

Posted: Tue May 09, 2006 2:11 pm
by pickle
The "validation" is really an ajax call to check if the date range entered already has data entered or not. Sending the focus to the field sounds like a good idea - I'll try that.

Posted: Wed May 10, 2006 5:20 am
by GM
Can't you manually call the Blur() event of the field after the Javascript update, so that the onChange event fires?

Posted: Wed May 10, 2006 9:46 am
by pickle
Yes I could, but that would require me changing the script that generates the calendar. As I mentioned, I might be using this JS library in other parts of this project, and I might not always want it to blur afterwards. I have updated the script to send the focus to the textfield it's updating - that seems like a more useful outcome.

Thanks - if this was a one-time usage I'd certainly give that a try.