Thuộc tính tagName trong HTML DOM trả về tên thẻ (tag name) của một phần tử HTML (luôn in HOA).
Cú pháp
element.tagName
Ví dụ
<!DOCTYPE html>
<html>
<head>
<style>
body { color: #000; height: 100vh; background-color: #FBAB7E; background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%); text-align: center; }
.btn { background: #db133a; border: none; height: 2rem; border-radius: 2px; width: 40%; display: block; color: #fff; outline: none; cursor: pointer; margin: 1rem auto; }
</style>
</head>
<body>
<h1>Demo element.tagName</h1>
<button class="btn" onclick="showTagName()">Click để xem tag name</button>
<div class="show"></div>
<script>
function showTagName() {
var tag = document.querySelector('button').tagName;
document.querySelector('.show').innerHTML = 'Tên thẻ là: ' + tag; // Trả về "BUTTON"
}
</script>
</body>
</html>
Kết quả
Nhấn nút → hiển thị "Tên thẻ là: BUTTON".
Lưu ý: tagName luôn trả về chữ HOA. Để lấy tên thẻ chữ thường, dùng tagName.toLowerCase(). Khác với nodeName (trả về tên node, với text node là #text).