JavaScript / Object / Element.getAttribute() / 요소의 속성 값을 가져오는 메서드
.getAttribute()
.getAttribute()는 선택한 요소(element)의 특정 속성(attribute)의 값을 가져옵니다.
문법
element.getAttribute( 'attributename' )
예를 들어
var jb = document.getElementById( 'xyz' ).getAttribute( 'title' );
는 id의 값이 xyz인 요소의 title 속성 값을 변수 jb에 저장합니다.
예제
id의 값이 abc인 a 요소의 href 속성의 값을 가져와서 출력합니다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> </head> <body> <p><a id="abc" href="https://www.codingfactory.net">Coding Factory</a></p> <script> var jbHref = document.getElementById( 'abc' ).getAttribute( 'href' ); document.write( '<p>href : ' + jbHref + '</p>' ); </script> </body> </html>