views:

27

answers:

3

OK, I'm sure I'm justing having a brain-fart after 2 weeks Honeymoon in Tuscany... by brains clear, and I'm googled out looking for keywords and how to do it, even scanned down the JQuery Valdate.js looking for the bit that does it...

What I want to do is this; but don't know where to start.

if ($("#errorMessage").exists()){

// Jump browser window down to #errorMessage.

}

A: 

scrollIntoView would probably solve your problems.

And .exists() would be .length > 0

David Hedlund
Yeh, got a besoke JQuery function that does exactly that, so I can just do an exists() - jQuery.fn.exists = function() { return jQuery(this).length > 0; }
Will Hancock
@Will - When writing a plugin just use `this.length`, `this` is already a jQuery object inside that context :)
Nick Craver
A: 

check scrollTop property. you will have to scroll to the position of the element

Elzo Valugi
Gotcha, thanks, pointed me in the right direction...
Will Hancock
+1  A: 

You can animate it down, like this:

if ($("#errorMessage").length){
  $("html, body").animate({ scrollTop: $("#errorMessage").position().top });
}

You can give it a try here, this just gives a nice transition to the element's positon.

Nick Craver