and i want to change the source of this element to be "./im" /> and i want to change the source of this element to be "./im" /> and i want to change the source of this element to be "./im"/>

How can I change the html img element source witch dart?

104 views Asked by At

I have this img element in my HTML project:

<img id="themeToggle" src="./images/moon.svg">

and i want to change the source of this element to be "./images/sun.svg".

I tried with:

void main() {
    var themeToggleButton = querySelector('#themeToggle');
    themeToggleButton?.onClick.listen((event) {
        themeToggleButton.dataset['src'] = './images/sun.svg';
    });
}

since the .dataset attribute is the only one that lets you to access the selected element's attributes, but it does not work. Any suggestion, please?

1

There are 1 answers

0
Mattia On

You should be able to use the .attributes getter to get the attributes Map and set there the value:

themeToggleButton?.attributes['src'] = './images/sun.svg'

Note: .dataset (as specified in the docs) is used only for the element properties that start with data-