change Icon color and background color based on select box using jquery

In this post i am going to tell you how to change icon background color and color based on the select box with the help of jquery and javascript. This is one of the simple methods just using if condition will done this concept with the help of javascript.



This type of concept is really helpful in some of the application development like badge customization or commendation bar. When you click the select box it opens a dropdown list and shows the list of colors, when you choose the bg color from this list it automatically applies the background color to the particular icon, Here i used the plus icon from the help of font awesome icon. and you can also choose the font color with the help of the other select box.

The below example have the codes you can customize this according to the your wish and also available demo page you can view the result of this program. I hope this post is really helpful to you.

Html Code for Change icon color and bg color using selectbox:

<h1>change Icon color and background color based on select box using jquery</h1>
<div class="color" style="width:205px; padding:66px; border:1px solid #f1f1f1;">
<i class="fa fa-plus fa-6 imgcolor" aria-hidden="true" style="font-size: 16em; "></i>
</div>
<select runat="server" style="background-color:#f1f1f1;color:#000;padding:15px;margin: 20px 0px 20px 0px;" id="select">
<option value="white" style="background-color: white;">Select background</option>
<option value="silver" style="background-color: silver;">Silver</option>
<option value="gold" style="background-color: #ffd700;">Gold</option>
</select>
<select runat="server" style="background-color:#f1f1f1;color:#000;padding:15px;margin: 20px 0px 20px 20px;" id="select-color">
<option value="black" style="background-color: white;">Select Icon color</option>
<option value="pink" style="background-color: pink;">pink</option>
<option value="purple" style="background-color: purple;">purple</option>
<option value="red" style="background-color:red;">Red</option>
</select>

Jquery for Change icon color and bg color using select box:

<script>
$('#select').change(function(){
if($(this).val() == 'silver'){
$(".color").css('background-color', '#e1e1e1');
}
if($(this).val() == 'gold'){
$(".color").css('background-color', '#ffd700');
}

if($(this).val() == 'white'){
$(".color").css('background-color', 'white');
}

});
</script>
<script>
$('#select-color').change(function(){
if($(this).val() == 'purple'){
$(".imgcolor").css('color', '#800080');
}
if($(this).val() == 'pink'){
$(".imgcolor").css('color', 'pink');
}

if($(this).val() == 'white'){
$(".imgcolor").css('color', 'white');
}
if($(this).val() == 'red'){
$(".imgcolor").css('color', 'red');
}
});
</script>

Post a Comment

0 Comments