aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/api/htmlinputelement/select/index.html
blob: 14354e31dc26021b1911ffe9c85ee57b24fda182 (plain)
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
---
title: HTMLInputElement.select()
slug: Web/API/HTMLInputElement/select
tags:
  - Auswählen
  - HTML
  - JavaScript
  - Méthode
translation_of: Web/API/HTMLInputElement/select
---
<div>{{ APIRef("HTML DOM") }}</div>

<p>Die <strong><code>HTMLInputElement.select()</code></strong> Methode selektiert den gesamten Text innerhalb eines {{HTMLElement("textarea")}} oder innerhalb eines {{HTMLElement("input")}} Elements welches ein Textfeld enthält.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox"><em>element</em>.select();</pre>

<h2 id="Beispiel">Beispiel</h2>

<p>Klick in diesem Beispiel auf den Button um den gesamten Text innerhalb des <code>&lt;input&gt;</code> Elements zu selektieren.</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;input type="text" id="text-box" size="20" value="Hallo Welt!"&gt;
&lt;button onclick="selectText()"&gt;Text auswählen&lt;/button&gt;
</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">function selectText() {
  const input = document.getElementById('text-box');
  input.focus();
  input.select();
}</pre>

<h3 id="Ergebnis">Ergebnis</h3>

<p>{{EmbedLiveSample("Beispiel")}}</p>

<h2 id="Anmerkungen">Anmerkungen</h2>

<p>Die Methode <code>element.select()</code> fokussiert den Input nicht zwingend, weshalb es oft zusammen mit {{domxref("HTMLElement.focus()")}} verwendet wird.</p>

<p>In Browsern in denen es nicht unterstützt wird ist es möglich es mit <a href="/en-US/docs/Web/API/HTMLInputElement/setSelectionRange">HTMLInputElement.setSelectionRange()</a> (mit den Parametern <em>0</em> und der <em>Länge des Werts des Inputs</em>) zu ersetzen.</p>

<pre class="brush: html">&lt;input onClick="this.select();" value="Beispieltext" /&gt;
&lt;!-- gleichbedeutend mit --&gt;
&lt;input onClick="this.setSelectionRange(0, this.value.length);" value="Beispieltext" /&gt;
</pre>

<h2 id="Spezifikationen">Spezifikationen</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Spezifikation</th>
   <th scope="col">Status</th>
   <th scope="col">Kommentar</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'forms.html#dom-textarea/input-select', 'select')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_Kompatibilität">Browser Kompatibilität</h2>



<p>{{Compat("api.HTMLInputElement.select")}}</p>

<h2 id="Siehe_auch">Siehe auch</h2>

<ul>
 <li>{{ HTMLElement("input") }}</li>
 <li>{{ HTMLElement("textarea") }}</li>
 <li>{{ domxref("HTMLInputElement") }}</li>
 <li>{{ domxref("HTMLInputElement.setSelectionRange") }}</li>
</ul>