views:

420

answers:

3

I have a simple bit of jQuery in doc ready which toggles a div containing a textarea:

$('div#addnote-area').hide(); // hide the div
$('a#addnote-link').click(function() { // click event listener on link
     $('div#addnote-area').toggle(); // toggle the hidden div 
});

The toggle works fine when clicking the link. The problem I'm having is that if div#addnote-area is below the browser's current viewport, it remains there when it's shown. I'd like the user's cursor to go to the textarea and for the whole textarea to be viewable in the window.

Click here to see an image for more context

+1  A: 

Check out the scrollTo jQuery plugin. You can simply do something like this:

$.scrollTo('div#addnote-area');

Or, if you want to animate it, provide the # of milliseconds for the scrolling to last:

$.scrollTo('div#addnote-area', 500);
Matt Huggins
@Matt Thanks, that does the trick! Wish there was a simple jQuery (non-plugin) way, but I'll take it.
k00k
A: 

jQuery's scrollTop also works. Try setting like:

 $('#idOfElement').css('scrollTop', 10)

You can get height/width pretty easily thru $(...).offset()

This is just from memory... :-)

Nathan Garrett
+1  A: 

Without the scrollTo plugin

$(window).scrollTop($('div#addnote-area').offset().top)
Ben