Fire event when textbox changes

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Fire event when textbox changes

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

poll the field. :)

setInterval()
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

hm, give it temporary focus? onChange="textarea.focus(); this.focus();"

[[ my attempt :) ]]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

that seems like a waste of resources...

why don't you fire it when the date is selected from the calendar?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Can't you manually call the Blur() event of the field after the Javascript update, so that the onChange event fires?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply