未加星标

Jquery - helps create the simplest color selector of all time

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Stackoverflow is awesome! I just posted a question and it was answered by the community within minutes and a working answer was obtained!

I have another question about a custom color picker. I know there are many color picker out there but it is just too complicated to use. I need a simpler one.

So I decided to create a simple 4x4 pallet of predefined color. The pallets are formed using div with different background color under a div-class named 'supercolor'.

<div class="supercolor" > <div class="colordiv" id="#111111" style="background-color:#111111;"> </div> <div class="colordiv" id="#222222" style="background-color:#222222;" > </div> <div class="colordiv" id="#333333" style="background-color:#333333;"> </div> </div>

In my script section, I manually add on click function to each and individual id to dynamically create an (input option check box checked html) and a tick character on the division so the user knows that color is selected. If the division is already checked, it will removed the (input option check box checked html) and the tick character.

I used an array to determined if the div is checked, and if it is, update that array index. The input check box are created so when the page submits, I have a way to know which color was selected based on the check box value which is the background color in hex.

var selected_arry = []; $('#111111').click(function(){ if (selected_arry == 1){ selected_arry[0] = 0; $('#111111').html(""); } else { selected_arry = 1; $('#111111').html("<input type='checkbox' name='selected_color[]' hidden checked id='#111111' />"); } }); ->repeat same code for next 15 divs but with different ID

My question is, I have to repeat this for all the division I create and it does look very unoptimized and I think there must be a better way to do it. I only have a few months of Jquery exposure and a newbie developer. I am hoping all the gurus out there can point me to a more efficient way.

Thank you very much!

Edit : Working Code

Finally with the help from @egis & @Rob Cowie, the code is completed with very efficient and scalable function. Note: I removed some part (making it simpler for beginner like me to understand) and edited some part to allow multiple selection.

CSS Code:

<style type="text/css"> .colour { width: 40px; height: 40px; float: left; cursor: pointer; line-height: 40px; text-align: center; } .red {background-color: #FF2622;} .green {background-color: #2B6DB9;} .blue {background-color: #4AFF41;} </style>

HTML Code:

<div id="colour-picker"> <div class="colour red"> </div> <div class="colour green"> </div> <div class="colour blue"> </div> </div>

Script Code:

$(document).ready(function() { $('.colour').click(function(event){ var $this = $(this); $this.toggleClass('selected'); if ($this.hasClass('selected')){ var colour = $this.css('backgroundColor'); $this.html("<input type='checkbox' name='selected_color[]' hidden checked id='"+colour+"' value='"+colour+"' />"); } else { $this.html(''); }; }); });

I've created a solution that delivers what I think you're after. See https://gist.github.com/962872 for the code.

In summary, clicking on colour divs toggles the class 'selected' and sets the value of a hidden form input (which will submit the colour).

If the clicked div is currently selected, the class is removed, and the value of the hidden input is set to empty.

You can do more on click simply by adding to the click handler.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: HTMLCSS
tags: gt,lt,div,color,selected,class,colour,background,id
分页:12
转载请注明
本文标题:Jquery - helps create the simplest color selector of all time
本站链接:https://www.codesec.net/view/582251.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(158)