VecchioGiacca
VecchioGiacca

Reputation: 133

Cannot set href and src of an element in js

I have this javascript code:

var footerDiv = document.getElementsByClassName("copyright")[0];

window.addEventListener('load', function() {
    var button = document.createElement('button')
    footerDiv.append(button)
    button.src = 'https://img.shields.io/badge/';
    button.href = "https://github.com/";
    button.id = 'srcButton';
})

but when i run the code the button doesn't appear (appears as a dot ".") and it doesn't have src and href properties.

please help me. thanks

Upvotes: 0

Views: 40

Answers (1)

Harshana
Harshana

Reputation: 5476

A button element doesn't have src and href attributes. Instead of that, you can use an image and anchor element to fulfill your task

var footerDiv = document.getElementsByClassName("copyright")[0];

window.addEventListener('load', function() {
    var a = document.createElement('a');
    a.id='srcButton';
    a.href = "https://github.com/AnonHexo/public-scripts";
    
    var img = document.createElement('img');
    img.src = 'https://img.shields.io/badge/%20-ComuBot--Tranlator-%23159818';
    a.append(img)
    footerDiv.append(a)
})
<div class="copyright"></div>

Upvotes: 1

Related Questions