onchange - if else on javascript with the value of a select box (pure javascript) -


i'm working on select box have images instead of text, (on background css).

<script type="text/javascript"> function onchange(element) { element.style.backgroundcolor = "yellow"; element.classname = "on_change"; } </script> <select onchange="onchange(this);"> <option value="1" style="background: url(/one.png) no-repeat scroll 0 0 transparent; width:32px; height:32px;"></option> <option value="2" style="background: url(/two.png) no-repeat scroll 0 0 transparent; width:32px; height:32px;"></option> <option value="3" style="background: url(/three.png) no-repeat scroll 0 0 transparent; width:32px; height:32px;"></option> </select> 

the problem how value of selected option , if 1 set 1 image , if 2 set image background using pure javascript (no jquery)?

i know selectedindex key problem, i'm clueless of how use or how use on if else statement.

the script above 1 of trials, use script above perform same task.

<select onchange="this.style.backgroundcolor=this.options[this.selectedindex].style.backgroundcolor; this.style.color=this.options[this.selectedindex].style.color"> 

here working example. http://codebins.com/codes/home/4ldqpb9

you dont need if else change background image based on selectedindex.

function onchange(obj) { obj.style.backgroundimage = obj.options[obj.selectedindex].style.backgroundimage } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -