forked from mdn/content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
142 lines (104 loc) · 4.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---
title: HTMLElement.focus()
slug: Web/API/HTMLElement/focus
tags:
- API
- Focus
- HTML DOM
- HTMLElement
- Method
- Reference
- Scroll
- View
- activate
browser-compat: api.HTMLElement.focus
---
<div>{{ APIRef("HTML DOM") }}</div>
<p>The <strong><code>HTMLElement.focus()</code></strong> method
sets focus on the specified element, if it can be focused. The focused element is
the element which will receive keyboard and similar events by default.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="brush: js"><em>element</em>.focus(<em>options</em>);</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>options</code> {{optional_inline}}</dt>
<dd>
<p>An optional object providing options to control aspects of the focusing process.
This object may contain the following property:</p>
<dl>
<dt><code>preventScroll</code> {{optional_inline}}</dt>
<dd>A Boolean value indicating whether or not the browser should scroll the
document to bring the newly-focused element into view. A value of
<code>false</code> for <code>preventScroll</code> (the default) means that
the browser will scroll the element into view after focusing it. If
<code>preventScroll</code> is set to <code>true</code>, no scrolling will
occur.</dd>
</dl>
</dd>
</dl>
<h2 id="Examples">Examples</h2>
<h3 id="Focus_on_a_text_field">Focus on a text field</h3>
<h4 id="JavaScript">JavaScript</h4>
<pre class="brush: js">focusMethod = function getFocus() {
document.getElementById("myTextField").focus();
}</pre>
<h4 id="HTML">HTML</h4>
<pre class="brush: html"><input type="text" id="myTextField" value="Text field.">
<p></p>
<button type="button" onclick="focusMethod()">Click me to focus on the text field!</button>
</pre>
<h4 id="Result">Result</h4>
<p>{{ EmbedLiveSample('Focus_on_a_text_field') }}</p>
<h3 id="Focus_on_a_button">Focus on a button</h3>
<h4 id="JavaScript_2">JavaScript</h4>
<pre class="brush: js">focusMethod = function getFocus() {
document.getElementById("myButton").focus();
}
</pre>
<h4 id="HTML_2">HTML</h4>
<pre class="brush: html"><button type="button" id="myButton">Click Me!</button>
<p></p>
<button type="button" onclick="focusMethod()">Click me to focus on the button!</button>
</pre>
<h4 id="Result_2">Result</h4>
<p>{{ EmbedLiveSample('Focus_on_a_button') }}</p>
<h3 id="Focus_with_focusOption">Focus with focusOption</h3>
<h4 id="JavaScript_3">JavaScript</h4>
<pre class="brush: js">focusScrollMethod = function getFocus() {
document.getElementById("myButton").focus({preventScroll:false});
}
focusNoScrollMethod = function getFocusWithoutScrolling() {
document.getElementById("myButton").focus({preventScroll:true});
}
</pre>
<h4 id="HTML_3">HTML</h4>
<pre class="brush: html"><button type="button" onclick="focusScrollMethod()">Click me to focus on the button!</button>
<button type="button" onclick="focusNoScrollMethod()">Click me to focus on the button without scrolling!</button>
<div id="container" style="height: 1000px; width: 1000px;">
<button type="button" id="myButton" style="margin-top: 500px;">Click Me!</button>
</div>
</pre>
<h4 id="Result_3">Result</h4>
<p>{{ EmbedLiveSample('Focus_with_focusOption') }}</p>
<h2 id="Specifications">Specifications</h2>
{{Specifications}}
<h2 id="Notes">Notes</h2>
<ul>
<li>If you call <code>HTMLElement.focus()</code> from a mousedown event handler, you
must call <code>event.preventDefault()</code> to keep the focus from leaving the
<code>HTMLElement</code></li>
<li>
<p>Behavior of the focus in relation to different HTML features like
{{HTMLAttrxRef("tabindex")}} or {{Glossary("shadow tree","shadow dom", 1)}},
which previously remained under-specified, were recently updated (as October
of 2019). Checkout <a href="https://blog.whatwg.org/focusing-on-focus">WHATWG
blog</a> for more info.</p>
</li>
</ul>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{domxref("HTMLElement.blur")}} to remove the focus from an element.</li>
<li>{{domxref("document.activeElement")}} to know which is the currently focused element.</li>
</ul>