开发者

Move Focus on an Input based on user's click?

开发者 https://www.devze.com 2023-04-12 09:30 出处:网络
I have a homepage, where you can either click Sign Up or Create An Account If you click either link it takes you to the same page, Sign up and Create An Account live on the same page.

I have a homepage, where you can either click Sign Up or Create An Account

If you click either link it takes you to the same page, Sign up and Create An Account live on the same page.

What they want is the Focus when you cl开发者_Python百科ick Sign Up to be in the Username Input field but if you click Create An Account the Focus First Name Input field.

How can I determine where the Focus should be based on what a User clicked on the previous page?


You can use hidden field or querystring. And on the document.ready of signup/create account page you can use focus().

HTH Ali


You could transfer this data with the url, js is able to read Strings after the Hash Sign '#' in the URL. Usually this is used for scrolling, but you can use it for focusing.

<script>
function getHash() {
  var hash = window.location.hash;
  return hash.substring(1); // remove #
}
function init() {
    document.getElementById(getHash()).focus();
}
</script>
<body onload="init();">
<input id="a" /><br/>
<input id="b" /><br/>
</body>

The Url would be

server.com/login.html#focusedElementId
0

精彩评论

暂无评论...
验证码 换一张
取 消