Beginner Friendly · Hands-on · Free

check if an element has a specified attribute or not

Javascript இல் hasAttribute() method இங்கு ஒரு குறிப்பிட்ட html element இல் நாம் search செய்த attribute அமைந்துள்ளதா என்பதை கண்டறிய பயன்படுகிறது

5 min read Updated Dec 13, 2025

JavaScript hasAttribute

Javascript இல் hasAttribute() method இங்கு ஒரு குறிப்பிட்ட html element இல் நாம் search செய்த attribute அமைந்துள்ளதா என்பதை கண்டறிய பயன்படுகிறது.

element.hasAttribute(name);

Note: இங்கு hasAttribute() method இல் இங்கு ஒரு குறிப்பிட்ட html element இல் நாம் search செய்த attribute அமைந்துள்ளதா என்பதை கண்டறிய பயன்படுகிறது. இங்கு நாம் search செய்யவேண்டிய attribute இன் key ஆனது argument ஆக அனுப்பப்படுகிறது. எனவே நாம் கொடுக்கும் argument ஐ பொருத்து குறிப்பிட்ட attribute ஆனது அமைந்துள்ளதா இல்லையா என்பதை கண்டறிய பயன்படுகிறது.

Example1


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JS hasAttribute() Demo</title>
</head>
<body>
    <button id="btnSend">Send</button>
<script>
        let btnSend = document.querySelector('#btnSend');
        if (btnSend) {
           var res = btnSend.hasAttribute('id');
           console.log(res);
        }
</script>
</body>
</html>

மேலே உள்ள Example1-ஐ கவனிக்கவும் இங்கு html tag கள் கொடுக்கப்பட்டுள்ளது. இங்கு document.querySelector('#btnSend') என்ற method மூலமாக நாம் ஒரு குறிப்பிட்ட element ஐ select செய்து கொள்கிறோம். பிறகு அதனை btnSend என்ற variable இல் store செய்து கொள்கிறோம். இங்கு btnSend.hasAttribute('id') என்ற hasAttribute method மூலமாக இங்கு id என்ற attribute இங்கு அமைந்துள்ளதா இல்லையா என்பதை கண்டறிய பயன்படுகிறது. இந்த element இல் id attribute ஆனது உள்ளது எனவே நமக்கு true என output கிடைக்கிறது.

Output:

<button id="btnSend">Send</button>

Example2


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JS hasAttribute() Demo</title>
</head>
<body>
    <button id="btnSend">Send</button>
<script>
        let btnSend = document.querySelector('#btnSend');
        if (btnSend) {
           var res = btnSend.hasAttribute('name');
           console.log(res);
        }
</script>
</body>
</html>

மேலே உள்ள Example2-ஐ கவனிக்கவும் இங்கு html tag கள் கொடுக்கப்பட்டுள்ளது. இங்கு document.querySelector('#btnSend') என்ற method மூலமாக நாம் ஒரு குறிப்பிட்ட element ஐ select செய்து கொள்கிறோம். பிறகு அதனை btnSend என்ற variable இல் store செய்து கொள்கிறோம். இங்கு btnSend.hasAttribute('name') என்ற hasAttribute method மூலமாக இங்கு name என்ற attribute இங்கு அமைந்துள்ளதா இல்லையா என்பதை கண்டறிய பயன்படுகிறது. இந்த element இல் name attribute ஆனது இல்லை எனவே நமக்கு false என output கிடைக்கிறது.

Output:

false

இது பற்றிய தங்களின் கருத்துகளை இங்கே பதிவிடுங்கள் . இது பயனுள்ளதாக விரும்பினால் மற்றவர்களுக்கும் இதை share செய்யுங்கள்.