2021年5月3日星期一

someone can tell me where i gone wrong javascript code

i have a code that changes the background colour or foreground colour but for some reasons it is not working. in this -i used a matrix colours for saving colours then using for loop created 5 buttons.on clicking of button the button value is saved in buttonValue html code

    <input type="checkbox" name="check" value="background">background</input>      <input type="checkbox" name="check" value="foreground">foreground</input>      <input type="button" value="button" id='run'>  

javascript code

var colors = ['red', 'blue', 'green', 'yellow', 'black'];    for(var i=0; i<5; i++){      var button = document.createElement('button');      button.type = 'button';      button.innerText = colors[i];      button.value = colors[i];      button.style.backgroundColor = colors[i];        button.addEventListener('click', change);      document.body.append(button);  }    function change(g){      var buttonValue = g.target.value;  }    document.getElementById('run').addEventListener('click',AlertMe);    function AlertMe() {      document.getElementsByName('check').forEach( (el) =>{      if(el.checked === true){          if((el.value) == "background")          {            document.body.style.backgroundColor = buttonValue;          }          else if((el.value) == "foreground")          {            document.body.style.color = buttonValue;          }                }           });     }  
https://stackoverflow.com/questions/67377570/someone-can-tell-me-where-i-gone-wrong-javascript-code May 04, 2021 at 09:08AM

没有评论:

发表评论