最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dropdown selector to change color of DIV using JS and CSS - Stack Overflow

programmeradmin2浏览0评论

What I need is a simple color picker (max 6-8 color) so the user can choose a color for an event that will be entered in a calendar.

I have tested several jQuery colopicker plugins and none have worked well in my project.

Either they are too plex (like Farbtastic) or are simple but don't run on the most recent version of jQuery.

So I decided to provide a good ol' dropdown menu with a list of colors

<select id="evt_color"> 
<option value="#3366CC">Blue</option> 
<option value="#E070D6">Fuchsia</option> 
<option value="#808080">Gray</option> 
<option value="#4bb64f">Green</option> 
<option value="#ed9d2b">Orange</option> 
<option value="#FF9CB3">Pink</option> 
<option value="#EA4A4A">Red</option> 
</select>

This works well. But I would like to give the user some feedback on the color they choose, before submitting the form.

I wonder if somehow we could create a small square DIV next to the dropdown, and have its background-color change to the value of the selected color onChange, via something like getElementById or other method. By default, the first color would be selected (Blue).

Anyone have suggestions for this? jQuery or raw JS suggestions are wele!

Thanks for helping!

What I need is a simple color picker (max 6-8 color) so the user can choose a color for an event that will be entered in a calendar.

I have tested several jQuery colopicker plugins and none have worked well in my project.

Either they are too plex (like Farbtastic) or are simple but don't run on the most recent version of jQuery.

So I decided to provide a good ol' dropdown menu with a list of colors

<select id="evt_color"> 
<option value="#3366CC">Blue</option> 
<option value="#E070D6">Fuchsia</option> 
<option value="#808080">Gray</option> 
<option value="#4bb64f">Green</option> 
<option value="#ed9d2b">Orange</option> 
<option value="#FF9CB3">Pink</option> 
<option value="#EA4A4A">Red</option> 
</select>

This works well. But I would like to give the user some feedback on the color they choose, before submitting the form.

I wonder if somehow we could create a small square DIV next to the dropdown, and have its background-color change to the value of the selected color onChange, via something like getElementById or other method. By default, the first color would be selected (Blue).

Anyone have suggestions for this? jQuery or raw JS suggestions are wele!

Thanks for helping!

Share Improve this question asked Apr 5, 2011 at 17:05 pepepepe 9,90925 gold badges117 silver badges192 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5
$(document).ready(function() {
    $("#evt_color").change(function() {
        $("#someDiv").css("background-color", $(this).val());
    }).change(); // trigger change event so div starts out with a colour
                 // on page load
});

You can try it here.

发布评论

评论列表(0)

  1. 暂无评论