[JavaScript]/[JavaScript] 개념 모음

[JS 개념] document.getElementById()

노트포미 2023. 4. 20. 00:14

document.getElementById()

: JavaScript에서 Id로 HTML에 있는 Element를 불러오는 함수

getElementById(id);

 

<예제>

html

<html lang="en">
  <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) {
  const elem = document.getElementById("para");
  elem.style.color = newColor;
}

1st 기록 : 23.04.20(목)