Here's the question (I understand stupid, but ...): how to set the focus on the desired element (for example, select) when you click the button? When I press a button, I just hide half of the form elements (so conceived), but it’s very inconvenient that we stay at the bottom of the page, but we need to be in the middle.
2 answers
Give your id
, for example hashcode
and
<button onclick="sample_click(this)"></button> <script> function sample_click() { var a = document.getElementById('hashcode') a.focus(); } </script>
|
First: determine the height of the page - screen.availHeight
.
Second: determine the y
coordinate of the element relative to the top of the page. The code can be taken, for example, at the coordinates of the element on the page .
Third: calculate y
so that the element is in the middle of the window.
Fourth: scroll the page - window.scroll(0, y);
.
Fifth (optional): give focus on the element - _элемент_.focus()
;
|