0

In the Javascript lecture pf CS50's Web Programming with Python and Javascript , there is an example where Brian tells that how to use dataset and fetch the value of it in the case of multiple buttion as seen below:

-----html code ----

Hello! Red Blue Lime

-------js code ---

document.addEventListener('DOMContentLoaded',() => {

document.querySelectorAll('button').forEach(button =>{
    button.onclick = () => {
        document.querySelector('#hello').style.color =button.dataset.color;
       

    }
});

});

But here instead of using the dataset property , if I use value property of the buttons , the color does not changes of the buttons

------html code----

Hello! Red Blue Lime

--------js code -----

document.addEventListener('DOMContentLoaded',() => {

document.querySelectorAll('button').forEach(button =>{
    button.onclick = () => {
        document.querySelector('#hello').style.color = this.value;
        

    }
});

});

  • In the case of the select drop down menu , this works perfectly as he later gave the example of the select menu in that lecture.

Can anyone please explain why this does not work in the case of multiple button .

1
  • Impossible to tell without seeing the html code. In colors1.html and colors2.html from the source zip, the buttons don't have value attribute. – DinoCoderSaurus Nov 11 '20 at 20:53

You must log in to answer this question.

Browse other questions tagged .