What is the best way to utilize a color picker to apply CSS color to a parent element?

When the pencil glyphicon is clicked, a color picker appears. I only want to apply the selected color from the color picker to the list elements that are the grand parent of the pencil glyphicon. Here's a snippet of my Ruby front-end code:

 <% categories.each(function (category) { %>
    <li id="<%= category.id %>" class="Round-boxes my-sortable">

      <% if(category.get("is_owner")) { %>   
        <div class="color-picker-pencil picker">
           <span class="glyphicon glyphicon-pencil"></span>
        </div>
      <% } %> 
    </li>
  <% }); %>

I am referring to this tutorial and have made some adjustments in the following JavaScript code:

JS code:

 applyColor: function(event) {
 
   myel = $(event.target).parent().parent()
   console.log(myel);
   $('.picker').colpick({
     layout: 'hex',
     submit: 0,
     colorScheme: 'dark',
     onChange: function(hsb, hex, rgb, myel, bySetColor) {

       $(myel).css('background-color:', '#' + hex);
     }
   }).keyup(function() {

     $(this).colpickSetColor(this.value);

   });

 }
What steps should I take to make this work as intended? Appreciate any guidance. Thanks!

Answer №1

There was a simple syntax error in the applyColor function that needed fixing. By making a small code change, I replaced the following code with the existing one:

    applyColor: function(event){
    $('.picker').colpick({
      layout:'hex',
      submit:0,
      colorScheme:'dark',
      onChange:function(hsb,hex,rgb,myel,bySetColor) {

        myel = $(myel).parent();
        $(myel).css('background-color','#'+hex);

      }

    }).keyup(function(){

    $(this).colpickSetColor(this.value);

    });

  }

After making this change, everything is now working perfectly fine. Take a look at the two lines of code inside the onChange function and compare them with the previous version.

Answer №2

Have you attempted to utilize the parent() method in Jquery?

onChange: function(hsb, hex, rgb, myel, bySetColor) {

       $(myel).parent('li').css('background-color:', '#' + hex);
     }

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

The Click Event Is Triggering Even with Correct Callbacks Being Set

I am struggling to understand why these functions are not executing properly. I know the correct syntax for binding a function, like this: $('#idOfThing').bind('click', foofunc); function foofunc() { ///do things } However, I am facin ...

Utilize Axios to send data in real-time to update any changes made to an input field in

Just wanted to write about this topic because I have a burning question. (Please note that the code in this post is just an example). I'm struggling with sending the input content of name_contact on each change without using a button. Is it even poss ...

What's the piece I'm overlooking? Attempting to append a class name to the HTML content retrieved from the server

After making an ajax request and receiving some html data, I attempted to add a class using the success method: <div id="test"> </div> Here is the code snippet from my success function: $('#test', response).addClass('mytest& ...

The image source is unable to locate the image

Adding an image to my React component is proving to be problematic as the alternative text keeps being displayed instead. In the Navbar component, this is how I am trying to include the image: <img src="../assets/images/nav_logo.png" alt=&quo ...

"Could you please include some additional information in the provided prompt

Automating the Window: https://i.sstatic.net/CEKpH.png Issue: Our team recently implemented an authentication process for our staging environment, which requires additional steps during testing. I discovered that 'prompt()' cannot be easily capt ...

The CSS does not display properly on iPad or mobile devices

Recently, I had the opportunity to design a website for a local music school. This project was a great learning experience that has shown me the importance of continuous improvement and practice. Check out the site here Initially, I wanted to make the w ...

What is the process for utilizing Angular's emulated encapsulation attributes on HTML elements that are dynamically added to an Angular component?

Within my custom Angular component, a third-party library is dynamically adding an HTML element. I am seeking a solution that can apply Angular's encapsulation attributes to these elements regardless of the specific third-party library being used. If ...

Examining Resolver Functionality within NestJS

Today I am diving into the world of writing tests for NestJs resolvers. I have already written tests for my services, but now it's time to tackle testing resolvers. However, I realized that there is a lack of documentation on testing resolvers in the ...

Having trouble with an unexpected value in your Angular2 Service? Don't forget to add

I encountered an error in my Angular2 app: Error: (SystemJS) Unexpected value 'ReleasesService' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation. Here is my AppModule code: import { NgModule } fr ...

The scrolling feature induces a contraction to the left side

Recently, I implemented the code below to fix my menu when scrolling the page: var num = 5; $(window).bind('scroll', function () { if ($(window).scrollTop() > num) { $('.scroll').css({'position':'fixed&apo ...

How do I control the opening and closing of my modal using redux state management?

When the user clicks on Checkout within the <CheckoutButton/> component below, the modal should appear. However, instead of this behavior, the modal is appearing when I reload my browser, which is not the expected outcome. What could be causing this ...

Vue: Develop a master component capable of displaying a sub-component

Is there a way to design a main component that is able to accept and display a subcomponent? Take the following scenario for instance: <Container> <Item/> <Item/> <SubMenu/> <Btn/> </Container> ...

Retrieving Dropdown Value in Bootstrap 5: How to Obtain the Selected Item's Value from the Dropdown Button

I am having a slight issue with my dropdown button where I am trying to display the selected item as the value of the dropdown button. The Flag Icon and Text should be changing dynamically. I have tried some examples but it seems that it is not working as ...

Transitioning from mui version 4 to version 5 leads to an error message: "TypeError: Cannot access 'keyboardDate' properties of undefined"

After updating from MUI v4 to version v5, I encountered failing tests with the following error: TypeError: Cannot read properties of undefined (reading 'keyboardDate') 17 | it("should render correctly without any errors", () =& ...

The arrangement of pictures on a card

I decided to tweak the original concept by placing the images inside a Bootstrap card. However, the end result is that the images appear to be floating. https://i.stack.imgur.com/Uf721.png Any suggestions on how to make them fit nicely within the card? ...

Having trouble receiving a JSON array after making an Ajax request

I have read through previous posts on this issue, but I still can't seem to figure it out. Hopefully, someone here can help me solve this problem. My challenge lies in retrieving an array, which I have encoded in json, from a PHP script and passing i ...

Incorporate Calendly Script into your NextJs application

I'm currently working on integrating Calendly into my Next.js project. However, I am unsure about the process. My goal is to embed it on a specific page rather than in _app or _document. Here is what I have attempted so far: import Script from &apos ...

Using React Quill JS and looking to log to the console when a change handler is triggered?

As I dive into the world of web development, I am currently working on crafting a blog dashboard. For the text editor, I've opted to use React Quill. While following the documentation, I came across a tutorial that includes an 'on change' ha ...

Guide to making a reusable AJAX function in JavaScript

Currently, I'm working on developing a function that utilizes AJAX to call data from another server and then processes the returned data using a callback. My goal is to be able to make multiple calls to different URLs and use the distinct responses in ...

Is there a way to stop a specific route in Express from continuing further execution without completing its function?

In my post route, I need to ensure that the user has provided all the necessary data in the body. To achieve this, I have added an if block to check for errors. router.post("/", (req, res) => { if(req.body.age < 24) { res.send("You are too you ...