> For the complete documentation index, see [llms.txt](https://westsilver.gitbook.io/study-script/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://westsilver.gitbook.io/study-script/javascript/web-browser/untitled/undefined.md).

# 제어 대상 찾기

문서를 자바스크립트로 제어하려면 제어의 대상에 해당되는 객체를 찾는 것이 제일 먼저 할 일이다. 문서 내에서 객체를 찾는 방법은 document 객체의 메소드를 이용한다.

### document.getElementsByTagName

: 인자로 전달된 **태그명**에 해당하는 객체들을 찾아서 그 리스트를 [NodeList](https://developer.mozilla.org/en-US/docs/Web/API/NodeList)라는 유사 배열에 담아서 반환한다.

<div align="center"><figure><img src="https://3193435092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Miu1tqKdNPM-BV0usUx%2Fuploads%2FGjVCbzF60R2R9TgG0h2L%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202023-02-08%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%204.11.53.png?alt=media&amp;token=a01ddbad-1ad9-4a6d-b5f5-8e0cc51c79f2" alt=""><figcaption></figcaption></figure></div>

{% code lineNumbers="true" %}

```html
<html>
<body>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<script>
    let lis = document.getElementsByTagName('li');
    for(let i=0; i < lis.length; i++){
        lis[i].style.color='red';   
    }
</script>
</body>
</html>
```

{% endcode %}

{% code lineNumbers="true" %}

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<ol>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ol>
<script>
    let ul = document.getElementsByTagName('ul')[0];
    let lis = ul.getElementsByTagName('li');
    for(let i=0; lis.length; i++){
        lis[i].style.color='red';   
    }
</script>
</body>
</html>
```

{% endcode %}

### document.getElementsByClassName

: **class 속성의 값**을 기준으로 객체를 조회한다.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li class="active">CSS</li>
    <li class="active">JavaScript</li>
</ul>
<script>
    let lis = document.getElementsByClassName('active');
    for(let i=0; i < lis.length; i++){
        lis[i].style.color='red';   
    }
</script>
</body>
</html>
```

### document.getElementById

: **id 값**을 기준으로 객체를 조회한다. 성능면에서 가장 우수하다.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li id="active">CSS</li>
    <li>JavaScript</li>
</ul>
<script>
    let li = document.getElementById('active');
    li.style.color='red';
</script>
</body>
</html>
```

### document.querySelector

: **css 선택자의 문법을 이용해서 객체를 조회**할수도 있다.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<ol>
    <li>HTML</li>
    <li class="active">CSS</li>
    <li>JavaScript</li>
</ol>
 
<script>
    let li = document.querySelector('li');
    li.style.color='red';
    let li = document.querySelector('.active');
    li.style.color='blue';
</script>
</body>
</html>
```

### document.querySelectorAll

: querySelector과 기본적인 동작방법은 같지만 **모든 객체를 조회**한다는 점이 다르다.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<ol>
    <li>HTML</li>
    <li class="active">CSS</li>
    <li>JavaScript</li>
</ol>
 
<script>
    let lis = document.querySelectorAll('li');
    for(let name in lis){
        lis[name].style.color = 'blue';
    }
</script>
</body>
</html>
```
