Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can I make jQuery go directly to <h2 id="id-name">?

I want to make jQuery navigates directly (no animation need) to a id that I pass in a variable.

I have various marks like id="content", id="edit", id="..." that are <h2> titles. Doing validation with PHP I will output a variable like var NAVIGATE_TO = <?php echo $where_failed;?> and I want to move the website to that id position.

Like if I do domain.tld/page#edit or #content but with jQuery because when I load the page my PHP framework doesn't allow me to indicate the hash.

like image 971
udexter Avatar asked Mar 04 '11 11:03

udexter


1 Answers

You can set location.hash to the id you need the browser to scroll to:

window.location.hash = '#edit';
like image 200
Yi Jiang Avatar answered Sep 21 '22 19:09

Yi Jiang