DOM

NonElementParentNode.getElementById() メソッド

 編集:2022.11.09 by T.A.

[Edge]。→

NonElementParentNode 内の指定の id 名に一致する要素を Element で返します。

構文
NonElementParentNode.getElementById(id)
NonElementParentNode Document
id ID を表す文字列。
戻り値

Element オブジェクト、または null。

関連

querySelector()

/mdn

HTML:

<html>
<head>
  <title>getElementById example</title>
</head>
<body>
  <p id="para">Some text here</p>
  <button onclick="changeColor('blue');">blue</button>
  <button onclick="changeColor('red');">red</button>
</body>
</html>

JavaScript:

function changeColor(newColor){
  var elem = document.getElementById('para');
  elem.style.color = newColor;
}

¶以下省く

HTML:

<SPAN id=IdTest>SPAN要素</SPAN>

JavaScript:

D=document.getElementById("IdTest") //=[object HTMLSpanElement]

HTML:

<SPAN id=Idあ>SPAN要素</SPAN>

JavaScript:

D=document.getElementById("Idあ"); //=[object HTMLSpanElement]

英数以外のid名でも取得が可能で、アンカーなどに利用できる。

Idあ.innerHTML="てすと";
てすと

と、なる。


テスト