Beginner Friendly · Hands-on · Free

set the value of a specified attribute on a element

Javascript இல் setAttribute() method இங்கு ஒரு குறிப்பிட்ட html element க்கு நாம் attribute ஐ set செய்வதற்கு பயன்படுகிறது

5 min read Updated Dec 13, 2025

JavaScript setAttribute

Javascript இல் setAttribute() method இங்கு ஒரு குறிப்பிட்ட html element க்கு நாம் attribute ஐ set செய்வதற்கு பயன்படுகிறது.

element.setAttribute(name, value);

Note: இங்கு setAttribute() method இல் இங்கு ஒரு குறிப்பிட்ட html element க்கு நாம் attribute ஐ set செய்வதற்கு பயன்படுகிறது. இங்கு attribute name மற்றும் value ஆகியவற்றை argument ஆக அனுப்புகிறோம். இந்த method மூலம் நாம் எளிதாக ஒரு html element க்கு attribute ஐ add செய்து விடலாம்.

Example1


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

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

Output:

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

Example2


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

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

Output:

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

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