본문 바로가기
WEB/Error

[jQuery][Javascript] Getting attribute of a parent node $(this).parentNode.attr('class')

개발은 글쓰기 부터
ppowerddev 2022. 1. 1.
반응형

 

Getting attribute of a parent node $(this).parentNode.attr('class')


 
jquery와 plain javascript를 섞어 섰다는것..
java 사용해서 백엔드단만 작업하다보니 전혀 생각못했던 부분이었다..
jquery를 사용하는곳이 아직 많기 때문에 해당부분을 유의해서 코드작업을 해야할것 같다.
 
// jQuery
$(this).parent().attr('class');
// Vanila Javascript
this.parentNode.getAttribute("class");

 

attr() 메서드와 getAttribute()메서드


.attr() 메서드

.attr(attributeName)

.attr(attributeName,value)

선택한요소의 속성의 값을 가져오거나 속성을 추가한다.

$( '#id' ).attr( 'class' );
$( '#id' ).attr( 'class','on' );

.getAttribute() 

.getAttribute('attributeName') 

선택한요소의 속성의 값을 가져온다.

var attr = document.getElementById( 'id' ).getAttribute( 'class' );

.setAttribute()

.setAttribute('attributeName','value') 

선택한요소의 속성의 값을 추가한다.

document.getElementById( 'id' ).setAttribute( 'class', 'on' )

 

Reference


반응형