How can I scroll to a form element without setting it focus?

39 views
Skip to first unread message

er...@craftti.com.br

unread,
Feb 28, 2013, 12:19:59 PM2/28/13
to chocolat...@googlegroups.com
Hi folks,

I have an form with multiple fields and when I validate it I'd like to have it scrolled to the first offending field, but I do not want to use focus in order to prevent the keyboard to shown up. Does any one knows a simple way to do it?

Thanks in advance,

Eric

Robert Biggs

unread,
Dec 3, 2013, 6:33:25 PM12/3/13
to chocolat...@googlegroups.com
Sorry for the late response. This is fairly simple to do, you first need to find out what the position of the element is in the document. You can do that by getting its offset top value, then set the window scrollTop value to that. The scrolling is not on the window, but on the article.

var input = $('#myInput');
var scroll = input.offset().top
$('#scrollableArticle')[0].scrollTop = scroll;
Reply all
Reply to author
Forward
0 new messages