{"id":2546,"date":"2011-01-13T20:33:56","date_gmt":"2011-01-13T15:03:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2546"},"modified":"2015-05-19T17:46:59","modified_gmt":"2015-05-19T12:16:59","slug":"jquery-script-to-put-focus-on-first-field-of-any-page","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/jquery-script-to-put-focus-on-first-field-of-any-page\/","title":{"rendered":"JQuery script to put focus on first field of any page"},"content":{"rendered":"<p>Hi,<\/p>\n<p>In my recent grails project, i needed to put focus on the first field of any page whenever the page loads. The requirement is such that if the page contains errors then the focus should be on the first input field which has errors.<\/p>\n<p>I searched a lot and with the help of my colleague we came out with a simple JQuery script to put focus on the first input field on any page whenever the page loads.<\/p>\n<p>The script i used is:-<\/p>\n<pre>\r\n   if (jQuery('.errors').size() &gt; 0) {\r\n       jQuery('input.errors:first').focus();\r\n       jQuery('.errors input:visible:first').focus();\r\n     }\r\n   else {\r\n      jQuery('input:text:visible:first').focus();\r\n    }\r\n\r\n\r\n<\/pre>\n<p>I put it in the layout and it works for every gsp which has that layout.<\/p>\n<p>&nbsp;<\/p>\n<p>It worked for me.<br \/>\nHope it Helps.<\/p>\n<p>Vishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In my recent grails project, i needed to put focus on the first field of any page whenever the page loads. The requirement is such that if the page contains errors then the focus should be on the first input field which has errors. I searched a lot and with the help of my [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[27,719,720],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2546"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2546"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2546\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}