简体   繁体   中英

Select Event from Drop Down Menu - Javascript Only

I have tried this a hundred ways, need some help getting going. I did look at other people's questions and answers but have been unable to use any of them and get them to work. I have a drop down menu. I need to create an event, changing the color in a div for each of the options in the drop down menu. Code is below, I need to use Javascript only not JQuery.

Here is the HTML for the "menu"

   <select name="colorBlog" id="colors">
<option value = "Red">Red</option>
<option value = "Blue">Blue</option>
<option value = "Green">Green</option>
<option value = "Yellow">Yellow</option>    
<option value = "Pink">Pink</option>
</select>

Try

function changecolor() {
    document.getElementById('mydiv').style.color = colors.value
}

var colors = document.getElementById('colors');
colors.addEventListener('change', changecolor);
changecolor();

Demo: Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM