Displaying a Color Sample in a Grid

I'm currently working on a feature that allows users to select colors using the jQuery minicolor widget. I really like how the color swatch appears inside the widget, but I'm having trouble replicating that same look in a data table. When I try to display a color sample in the table, all I get is a tiny dot instead of a nice rectangle swatch.

Is there a way to achieve a similar visual representation of the selected color in a data table as seen in the minicolor widget?

// Hide all stuffs
$('.rw_widget_colour').addClass('rw_hidden');

// Init minicolors
$('.text').minicolors();

// Set color
$('.colorpicker').minicolors('value', '#2ed1c1');

// Show
$('.rw_widget_colour').removeClass('rw_hidden');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://labs.abeautifulsite.net/jquery-minicolors/jquery.minicolors.css">
<script src="http://labs.abeautifulsite.net/jquery-minicolors/jquery.minicolors.js"></script>    


<form class="aui">
  <label class="rw_widget_colour">
    <div class="rw_widget_swatch">jaoja</div>
    <input class="text colorpicker" type="text" name="rw_widget_colour" value="#ff0"/>
  </label>
</form>

<!-- Show a swatch: -->

<table>
  <tr><td>
    <span class="minicolors-swatch minicolors-sprite minicolors-input-swatch">
      <span style="background-color: rgb(46, 209, 193);" class="minicolors-swatch-color"></span>
    </span>
  </td></tr>
</table>

Answer №1

I have attempted to address the question with an answer that aligns with your needs and expectations.

Essentially, when the color picker changes its value (color), we extract the newly selected background color from the generated span and apply it as the attribute for a table cell td.

You have the flexibility to customize this solution according to your requirements or provide additional clarifications on any misunderstood aspects of the question.

UPDATE:

In response to feedback, I have added the display of the color code next to the color sample (now enclosed within a div - you can adjust the sizes as needed)

$(function() {
      // Hide all elements initially
      $('.rw_widget_colour').addClass('rw_hidden');

      // Initialize minicolors
      $('.text').minicolors();

      // Set default color
      $('.colorpicker').minicolors('value', '#2ed1c1');

      // Display elements
      $('.rw_widget_colour').removeClass('rw_hidden');

      // Update table color based on color picker changes
      $(".colorpicker").on('change', function() {
        var newCol = $(this).parent().find('.minicolors-swatch-color').attr('style');
        var newColText = $('input.text.minicolors-input').val();
        $('.colourChosen').attr('style', newCol);
        $('.colourChosenText').text(newColText);
      });
    });
.rw_hidden {
  display: none;
}
td div {
  min-width: 25px;
  width: 25px;
  min-height: 25px;
  height: 25px;
  display: inline-block;
}
td {
  width: 120px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="https://cdnjs.cloudflare.com/ajax/libs/jquery-minicolors/2.2.6/jquery.minicolors.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-minicolors/2.2.6/jquery.minicolors.min.js"></script>

<!--
<link rel="stylesheet" href="http://labs.abeautifulsite.net/jquery-minicolors/jquery.minicolors.css"> 
<script src="http://labs.abeautifulsite.net/jquery-minicolors/jquery.minicolors.js"></script>
-->
<form class="aui">
  <label class="rw_widget_colour">
    <div class="rw_widget_swatch">jaoja</div>
    <input class="text colorpicker" type="text" name="rw_widget_colour" value="#ff0" />
  </label>
</form>

Show a swatch:

<table>
  <tr>
    <td>
      New Color:
    </td>
    <td>
      <div class="colourChosen"> </div>
      <div class="colourChosenText"></div>
    </td>
  </tr>
</table>

Answer №2

If you're looking for a way to implement color picking functionality on your website, consider using the colorpicker jQuery plugin.

This plugin allows users to easily select and change colors by clicking on a customizable color box. The default color can be set according to your preferences.

Check out the code snippet below which demonstrates how to use the colorpicker plugin:

https://i.sstatic.net/vZwGS.png

$('#colorSelector').ColorPicker({
    color: '#0000ff',
    onShow: function (colpkr) {
    $(colpkr).fadeIn(500);
    return false;
    },
    onHide: function (colpkr) {
    $(colpkr).fadeOut(500);
    return false;
    },
    onChange: function (hsb, hex, rgb) {
    $('#colorSelector div').css('backgroundColor', '#' + hex);
    }
    });
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://www.rachelgallen.com/css/colorpicker.css" type="text/css" />
    <link rel="stylesheet" media="screen" type="text/css" href="http://www.rachelgallen.com/css/layout.css" />
    <title>ColorPicker - jQuery plugin</title>
<script type="text/javascript" src="http://www.rachelgallen.com/js/jquery.js"></script>
<script type="text/javascript" src="http://www.rachelgallen.com/js/colorpicker.js"></script>
    <script type="text/javascript" src="http://www.rachelgallen.com/js/eye.js"></script>
    <script type="text/javascript" src="http://www.rachelgallen.com/js/utils.js"></script>
    <script type="text/javascript" src="http://www.rachelgallen.com/js/layout.js"></script>
</head>

<body>
<div id="colorSelector">
  <div style="background-color:#ff00ff"></div>

  </div>
</body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Activate the button with a tap of the space bar on a webpage

Is it possible to have a button on a webpage triggered both by clicking with the mouse and hitting the spacebar? HTML <div class="col-12 button small-button"> <a onclick="generator()"> <img src="icones%20web%20projeto.p ...

Welcome to the interactive homepage featuring multitouch authentication

I am looking to design a homepage that is interactive based on touch input. For instance, if the user uses 5 fingers to touch the screen, similar to the green circles in the image below, they will be redirected to a specific homepage. If they touch in a di ...

What is the most effective method for coding an input tag with specific restricted characters?

Introduction I have a unique idea for creating an input field of fixed length where users can fill in the gaps without modifying certain pre-filled characters. For example, I want to display "__llo w_rld!" and let users complete the missing characters. In ...

Creating a custom dropdown behavior using Angular

I have a unique interface requirement where users must be able to click on an element to view its options, which are displayed as checkbox inputs. Essentially, I need a custom 'select' element that allows for multiple checkbox selections. ...

The challenges of concealing an AJAX DIV element

Below you will find my jQuery snippet: $('a[rel=close]').click(function() { alert('Click to close!'); $('div#purchasePanel').hide(); }); This script is utilized in conjunction with: <div id="purchaseP ...

Personalize the appearance of dynamically generated DIV elements

This script generates a random number of squares (ranging from 20 to 40) and adds text to each square. The script then calculates the width of each square so that they all fit in a single row. Here is the code snippet: var quantity = Math.floor(Math.ran ...

Activate audio when the link is clicked

Recently, I created a compact web application and it's almost complete. However, there is one cool functionality that I am missing. My goal is to have a sound play when the user clicks a link, but after playing, I want the navigation to continue as us ...

Display a progress bar in Bootstrap with a labeled indicator positioned above the bar and background to the

Is there a way to create a progress bar with a label positioned to the right using Bootstrap v5.2 Progress Bar? https://i.sstatic.net/gOWie.png I attempted to use negative margins to achieve this layout but faced issues when the label expanded due to lon ...

The concept of CSS sprites and managing background positions

I have been working on integrating a star-rating widget that requires the use of a sprite file. The sprite file I am using looks like this: https://i.stack.imgur.com/ZSMMj.png This is how my HTML is structured: HTML <span id="star-ratings" class="c ...

Revise the text while keeping the column content unchanged

Is it possible to change the text in a column without affecting the image using Jquery? Can this be achieved solely with Jquery without any modifications to the HTML code? Check out the demo $(".jsNoWrap").text("Change text"); <script src="https://a ...

Why isn't my API's JSON Response showing up in PHP?

Having trouble displaying the JSON response from an API call in PHP. Despite confirming that the server, IP settings, and API status are all fine on the provider's end, I am unable to identify the problem. This is how I'm handling the PHP part: ...

Ways to access the content within a div tag

Is there a way to retrieve the ID of the textarea when clicking on the image within the anchor and div tags? Here is the relevant HTML code: <img src="Image/icons/preview.png" alt="Preview" id="img1" onclick="ViewHtml(this.id);" /> <a id="ancH ...

Determining the starting and ending position of text within an element using jQuery

Delving into the world of jQuery/JS has been both challenging and exciting for me. However, I've encountered a problem that's got me stumped. Here is a snippet of text that I need to work with: blablablabla <b data-start="" data-end="">#fo ...

Manipulate paths in JS/JQuery by dynamically adding parameters with an indefinite number of values

My JavaScript script relies on an AJAX call that adds a parameter to an HTML request, but I've encountered two issues. I'm struggling to find a straightforward method to retrieve the pathname with parameters. With pure JavaScript, location.path ...

How to eliminate spaces while preserving line breaks in a textarea using JS or jQuery

I have been trying to figure out how to remove extra spaces from the beginning and end of lines while keeping the line breaks intact. Here's what I attempted: function removeSpaces(string) { return string.split(' ').join(''); } ...

Adjusting image width using jQuery

I have been experimenting with creating a Webgl hover effect for an image. The effect works well, but now I am trying to specify a width for the image within jQuery. new hoverEffect({ parent: document.querySelector('.ticket'), intensity1: 0. ...

Attention! Are you aware of the potential consequences of the impending phase-out of the name attribute in W3C validation? Have you considered how this development

Any thoughts on what could replace the name attribute? According to the W3C validator... Validation Output: 4 Warnings Here is a list of the warning message(s) generated while checking your document. Warning Line 12, Column 21: The name attribute i ...

What steps can I take to ensure my HTML5 mobile web app is optimized for compatibility across a variety of devices

I recently developed an HTML5 mobile web app and conducted tests on various devices including iPhones, iPads, Android phones, and tablets. What steps can I take to optimize the size and resolution of this app for seamless performance across all these dif ...

CSS - when "start" is not 1, an alternative to the "nth-child" selector

Imagine a scenario where you have a list of questions that start at 0 and you want to apply CSS styling based on their value. For instance, consider the following list... <b>How are you feeling?</b> <ol start="0"> <li>Great< ...

The image will only display upon receiving a link, not a path

Having some trouble displaying an image on my website, despite having successfully done so in the past for other projects. The image is located in the same folder as my HTML file. Here's what I've tried: <img src="reddit.png"/> <img s ...