views:

753

answers:

3

I have HTML form with a lot of inputs. I'm interesting in situation when I have focused input, after that I scroll page and focus is messed.

+1  A: 

Problem: a focussed element loses focus when the IE scrollbar is used.

You'd need to:

  • identify the element that loses focus
  • identify when the document has been scrolled
  • re-apply the focus

You can identify the element that loses focus through the blur event. Add a suitable event handler to every element where you want the focus to be remembered.

When the blur event handler runs, for a given input element, you should store, perhaps in global variable, an identifier for the input element. How you do this depends on how you choose to identify the element. The id of the element would be ideal.

You can tell when a document is being scrolled through the scroll event handler of the window object. See https://developer.mozilla.org/en/DOM/window.onscroll.

You can tell when a document has finished scrolling by comparing the before and after values of scrollHeight. See https://developer.mozilla.org/en/DOM/element.scrollHeight.

Overall:

  • add blur event handlers to your input elements
  • on blur, note the element that loses focus and the current scrollHeight
  • during scrolling, compare scrollHeights to determine if scrolling has stopped
  • when scrolling has stopped, re-apply the focus

You may want to wait a short period between the scrolling having stopped and re-applying the focus. The user might be scrolling a little, stopping, scrolling, stopping. You don't want your script to cause crazy focus hopping behaviour.

Jon Cram
A: 

I think you'd need to ask youself the question whether the browser you are using (and its version) are up-to-date with todays standards... For example, FF, Safari, Chromium (or Google Chrome) and my other 'next-gen' browsers do not lose focus when using a scrollbar.

If you're using, let's say, IE4, than just stop trying to improve on its lacking features. However, I don't know which one and version you are using so if it's the latest IE. You could just try as Jon Cram states.

You are the one who should klnow if the user percentage of browser lacking the features you want is big enough to let you implement changes in these browsers. If you do, please mind to only mimplement these features for the browsers lacking them or you'll be taking away control from the browsers that DO have sufficient implementation of 'next-gen' functions.

xaddict
:) I agree with you, but problem still not resolved. Thanks
omoto
A: 

Anyone with a real solution or hack or ....?

knibals