SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [resolved] Per character typed event for a text input field (FireFox issue)

    I have a text input field with a nearby listbox showing the results of a search performed on the input field text.

    I want the search result to update realtime as the user is typing in characters. The more specific their search term, the shorter the results list would become. I am searching an xml and it is small enough that speed is not an issue.

    The problem is finding the appropriate event trigger in javascript for FireFox. IE has onPropertyChange which I can put on the input field which works flawlessly for what I want. I want to duplicate that in FireFox.

    Worst case I could setup a timed event every second or so that checks if the field value has changed, and if it has, call the updateresults() function. It isn't really a bad solution, when there is no change to the field there would be very little overhead to the event. Is there a better way around this problem that I am missing?
    Last edited by Alluvian; May 2, 2008 at 11:04.

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Try onkeydown, onkeyup and onchange, all together. The problem is, there isn't really one that can get every single possibility, but with those it's pretty close.

    onkeydown gets each hit of the key.

    onkeyup is there because if you were to press a key and hold it down, it would constantly write letters, but only the first character would react with onkeydown, so onkeyup will catch it once they let go.

    onchange is there just as a safety net for any other possible changes.

    Those are what I'm using for something else that needs to update in as close to real-time as possible.

    Hope that helps.

  3. #3
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good idea. I had already written the timer event version, but this will work a little better.

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    You could even couple this with the timer version if you really wanted. You could have it check it ever 1 or 2 seconds, but if any of the other events go off, reset the timer to 0 and start again.

    Of course, this level of checking may be overkill depending what you are working on, and depending the page, it could sit there checking over and over even when they haven't made a change in hours.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •