Click event in jQuery to change the background color of <td> element

I'm still getting the hang of jQuery, so please bear with me. :)

The purpose of this code is to color a square with the selected color when clicked if it's white, and turn it back to white if it's already colored. It works fine except for an issue where there's a white spot in between when transitioning from one color to another. For example:

When switching from black to blue, it goes black > white > blue.

Below is a snippet of my code:

$(pixelCanvas).on("click", "td", function(){
let background = $(this).css("background-color");
let isEmpty = !background || background === "rgb(255, 255, 255)" ||
   background === "#ffffff";
let isNotColor = background !== color;
if(isEmpty){
    $(this).css("background-color", color); 
} else if(isNotColor){
    $(this).css("background-color", color);
} else {
    $(this).css("background-color", "#ffffff");
}

Link to my codepen:

https://codepen.io/ashleighc207/pen/QaezPO

Thank you in advance!!

Answer №1

After discovering a function to convert RGB to hex on Stack Overflow, I decided to try it out on the background color. It appears to be functioning correctly in Chrome on Windows 10, but I'm uncertain about its consistency across all browsers and operating systems. Nonetheless, this exercise highlights the importance of ensuring uniformity in color code formats.

/* global $ */
$(document).ready(function() {
  // JavaScript logic for creating pixel grid and coloring 
});
body {
  margin: 0px;
  margin-bottom: 500px; /* added to prevent logs from blocking screen */
}

.container {
  // CSS styles for container element
}

.row,
.column {
  background-color: #FFFFFF;
}
// More CSS styling here

<!DOCTYPE html>
<html>
  <head>
    <title>Pixel Art</title>
    <link rel="stylesheet" href="stylesheet.css">
  </head>

  <body class="container">
    // HTML markup for Pixel Art Maker component
  </body>
</html>

EDIT: Included specific browser and OS details to enhance clarity and added margin to avoid interference with inputs due to log display.

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

Having trouble passing a JavaScript variable through AJAX requests

In this particular script, I am encountering an issue where the variable "encrypted" is expected to be sent via ajax to upload.php, however I am unable to achieve this. Oddly enough, if I substitute the "encrypted" variable with another variable in the a ...

Codeigniter form not receiving input file data through jQuery ajax post request

Need help with uploading a file to the server using CodeIgniter framework and jQuery ajax. When submitting the form, the image field returns empty and the picture does not upload. Your assistance is greatly appreciated. Below is the code: Controller: p ...

Wait for the page content to finish loading before loading the Facebook Like Box

I am currently working on a method to delay the loading of the Facebook Like Box until after the page content has been fully loaded. My approach involves using AJAX, but I have encountered mixed results thus far. Initially, I used the following code snippe ...

Unable to modify the content of a table cell after triggering a Bootstrap modal event

Looking to update values in rows of a table? Here is an example table: I need to update the title per selected row. After selecting the first row, changing the value works fine. However, when I choose the second row, the issue arises where the value from ...

Desktop Safari displays Font-awesome icons with trimmed corners using a border-radius of 50%

While working on incorporating font awesome share icons into my project, I encountered an issue with getting a circle around them. After exploring various approaches, I opted for a CSS solution. Using React FontAwesome posed some challenges that led me to ...

Incorporate text directly into Bootstrap select input on a single line

I am attempting to include an asterisk in a select input field. I can achieve this easily by applying my own CSS, but the challenge is to accomplish this using Bootstrap 3.3.7 or an older version while displaying the asterisk on the same line as shown in t ...

creating dynamic column headers with JavaScript

I am looking for a way to make the column names dynamic so that I don't have to manually update them every time. Here is my code snippet: jqGrid11.prototype = { display : function() { $('body').append(this.html.join("")); $("#jqGrid").j ...

Efficiency in Javascript coding techniques

Hey there, I'm seeking some feedback on the efficiency of my aspect ratio function. This function is designed to determine the aspect ratio and limit the size of an image. Take a look and let me know what you think! function constrainTwoNumbers(optio ...

In the readmore.js script, position the "readmore" link within a div instead of outside of it

I have added annotations that vary in length, so I am looking to integrate the readmore.js plugin. To ensure uniform sizing for all annotations (even empty ones), I need to set a minimum height for the div container. <annotation> <div style="wi ...

Modifying the div based on the color it is positioned in front of

I am attempting to create a sticky, transparent div that changes its color based on the background behind it. I have both dark and light colored divs, and I want the sticky div to adjust its text color accordingly (white on dark backgrounds, black on light ...

Automatically submitting Ajax form upon loading the page

I am attempting to use ajax to automatically submit a form to a database upon page load. The form and php code work perfectly when manually submitted, but for some reason, the ajax function does not trigger. Despite checking the console for errors and con ...

Is it impossible to style an element within a div tag?

How can I apply styling to a specific div with id="1", which contains class="match"? Here is the code: <div class="col-lg-3"> <div id="1"> <p class="match">Match {this.state.matches[0].id}</p> <div class="tea ...

Having difficulty interpreting $ .ajax data within the controller

I am currently working on a jQuery .ajax project where the user interacts with a button in the view, triggering a $.ajax request. The goal is to send the user's name to a controller, which will then add "Hello" in front of the name and send back a JSO ...

How can Node.js and Socket.io be used with either PHP or jQuery to search within a text file?

I currently have a setup where I am using node and socket io to transmit a dynamic time format (HH:MM:SS) to my apache-served index.php file. I have successfully implemented the functionality to display the time in the PHP file. Now, my goal is to dynamic ...

Styling Angular Datatables with CSS

i have data on the front end https://i.stack.imgur.com/2xq7a.jpg i need all check marks to be displayed in green color. Below is the code snippet: $scope.dtColumnsCal= [ DTColumnBuilder.newColumn('name').withTitle('Name') ...

The success function of the ajax request is not initiating until I halt the page

I am currently working on a server using the Django 1.4 development environment on Open-SUSE. The Issue I'm Facing Although my jquery ajax request is triggered, the success function does not execute until I manually stop the page in my browser. Att ...

Ways to differentiate between a desktop browser width of 1024px and a tablet width of 1024px with the help of jquery

So here's the issue I'm dealing with: I have scroll-based animation functions set up for desktop browsers, and different animations in place for tablet browsers. The challenge is to make these functions work on a desktop with a 1024px dimension. ...

Stylish popup image with a link to an external source

Currently, I am utilizing Fancybox 3 and Cookie in order to display hidden content once the page has finished loading. The concealed content consists of both an image and an external link. I am encountering difficulties with regards to the functionality ...

Could someone provide an explanation for this Jquery?

$(document).ready(function () { if ((screen.width >= 1024) && (screen.height >= 768)) { alert('Screen size: 1024x768 or larger'); $("link[rel=stylesheet]:not(:first)").attr({ href: "style2.css" ...

Can you provide guidance on transforming a JSON date format like '/Date(1388412591038)/' into a standard date format such as '12-30-2013'?

I have a json that is created on the client side and then sent to the server. However, I am facing an issue with the conversion of the StartDate and EndDate values. Can someone please assist me with this? [ { "GoalTitle": "Achievement Goal", ...