How to change background color multiple times with one button (only JavaScript please)? -


i'm kinda new javascript , i'm trying figure out how have 1 button changes background color on click. @ moment can 3 three separate buttons, don't know how one.

on click want next color in list selected.

i have no knowledge on jquery, appreciate if code isn't in jquery.

this have @ moment:

<button onclick="myfunction1()">red</button> <script type="text/javascript">function myfunction1() { document.body.style.backgroundcolor = "red"; }</script>  <button onclick="myfunction2()">blue</button> <script type="text/javascript">function myfunction2() { document.body.style.backgroundcolor = "blue"; }</script>  <button onclick="myfunction3()">green</button> <script type="text/javascript">function myfunction3() { document.body.style.backgroundcolor = "green"; }</script> 

const changecolor = document.getelementbyid('changecolor'),        colors      = ['red', 'green', 'blue'];  let   colorindex  = 0;    changecolor.addeventlistener('click', () => {    document.body.style.backgroundcolor = colors[colorindex];          colorindex = (colorindex + 1) % colors.length  });
<button id="changecolor">changecolor</button>


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -