setAttribute 메서드
setAttribute 메서드를 사용하면
특정 태그의 지정된 속성 값을 변경할 수 있습니다.
구문
element.setAttribute(attribute name, new value);
예제
요소의 value 속성 값을
변경해 보겠습니다:
<input id="elem" value="abcde">
let elem = document.querySelector('#elem');
elem.setAttribute('value', '!!!');
HTML 코드는 다음과 같이 보일 것입니다:
<input id="elem" value="!!!">
관련 내용
-
속성을 가져오는 메서드
getAttribute -
속성을 제거하는 메서드
removeAttribute -
속성을 확인하는 메서드
hasAttribute