Is there a way to obtain the color value when duplicating the input field in the Bootstrap colorpicker? I am seeking assistance with this

Having trouble getting the color value of a duplicated input field in Bootstrap Colorpicker. Any suggestions on how to achieve this? Please assist me with this dilemma.

<a href="#" class="btn btn-default" id="cp4">Change background color</a>

<a href="#" class="btn btn-default" id="cp4">Change background color</a>
      <script>
          $(function () {
              $('#cp4').colorpicker().on('changeColor', function (e) {
                  $('body')[0].style.backgroundColor = e.color.toString('rgba');
              });
          });
      </script>

To view the original code, please visit:

Your help is greatly appreciated!

Answer №1

Unfortunately, I am unable to test this out at the moment. Feel free to give it a try:

<a href="#" class="btn btn-default cp4">Change background color</a>
<a href="#" class="btn btn-default cp4">Change background color</a>

$(function() {
  $('.cp4').colorpicker().on('changeColor', function() {
    var value = $(this).colorpicker('getValue');
    // implement your desired action here
  });
});

Answer №2

When commenting, it is important to note that each button must have a unique ID. This way, you can assign a class to each button or wrap both buttons in a parent element with an ID and then call the function when the .btn class within the div is clicked.

<div id="cp4">
  <a href="#" class="btn btn-default" >Change background color</a>

  <a href="#" class="btn btn-default">Change background color</a>
</div

//js
      <script>
          $(function () {
              $('#cp4 .btn').colorpicker().on('changeColor', function (e) {
                  $('body')[0].style.backgroundColor = e.color.toString('rgba');
              });
          });
      </script>

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 text font family isn't displaying the £ symbol accurately

On my webpage, I have decided to use the font "Century Gothic, Arial, Courier New, Sans-Serif" for my text. However, whenever I include the £ character, it does not display correctly on various browsers like Firefox, Explorer, Chrome & Safari. HMTL: < ...

What is the best way to determine the final letter of a column in a Google Sheet, starting from the first letter and using a set of

My current approach involves generating a single letter, but my code breaks if there is a large amount of data and it exceeds column Z. Here is the working code that will produce a, d: const countData = [1, 2, 3, 4].length; const initialLetter = 'A&a ...

Can we implement CSS that is based on the latest ancestor?

Incorporating CSS, my goal is to implement varying styles to the element labeled with the class test. However, the style should differ based on whether it is situated within an element of class a or class b: <div class="a"> lorem <div class=" ...

The Cordova Network Information Plugin is experiencing some functionality issues

I successfully developed a Mobile application using Cordova, Onsen UI, and Vue.js. While addressing network connectivity issues, I incorporated the cordova plugin cordova plugin add cordova-plugin-network-information For determining the type of connectio ...

What are the steps for transforming this code into pure CSS, or incorporating it into a Javascript function?

@function generate-random-box-shadows ($n) $value: '#{random(2000)}px #{random(2000)}px #FFF' @for $i from 2 through $n $value: '#{$value} , #{random(2000)}px #{random(2000)}px #FFF' @return unquote($value) $random-shadows1 ...

Is it possible to generate a separate table for each user using PHP?

Embarking on a new adventure, I have chosen to create an online shopping website for a college project. My roadblock comes at the sign-up stage. I envisioned users having separate tables to store products they add to their carts, allowing them to add more ...

jQuery: The duration parameter does not work in the .animate() function

My attempts to adjust the duration parameters in the code below (50, 1000, 4000) seem to be ineffective. Regardless of the duration value I set, the animation appears to run at a speed that is approximately half a second. For a live demonstration, please v ...

webpack is failing to load SVG files

My application structure includes: /webapp /lib /assets ic_add_black_24px.svg ic_clear_black_24px.svg .. .. The configuration in my webpack.config.js looks like this: var path = require('path'), webpack = requ ...

Optimal method for adjusting URL parameters using JavaScript

I am currently exploring different methods to modify URL parameters, but I haven't found the perfect solution yet. Initially, I attempted to achieve this using the following code: window.location.href = window.location.href + '?test'; Unf ...

Obtaining data objects with Angular 2 from JSON

Recently, I received a URL that includes data arrays in JSON format. My goal is to retrieve and utilize all elements within it: However, when attempting this, I end up with everything but nothing specific. For instance: How can I access data.name or data. ...

The functionality of Node.js middleware is not functioning correctly

My module contains Routes, and I want to verify access before allowing users to proceed. Within the Routes module, there are two routes and two access-check functions that I want to use as middlewares: const checkUser = (req, res, next) => { if (!us ...

Trouble implementing array filter in React component is a common issue

Hello everyone! I'm facing an issue with deleting an element from my useState array. I have the index of the element that I want to remove, and I've tried the following code snippet: const updatedArray = myArray.filter((item: any, index: number) ...

Is there a way for me to conceal my table upon clicking on the sidebar? Additionally, when I click on a button to display a different table, can the currently visible table automatically close?

I have a unique table for each button. Initially, the tables are hidden using CSS visibility: 'hidden', and when I click a button, the corresponding table displays. However, the issue arises when I click the same button again as it fails to hide ...

Seeking assistance in retrieving XML data from a separate domain

I am facing a challenge with a webservice located on a separate domain, specifically an authentication servlet delivering data in XML format. Whenever I attempt to use an XMLHttpRequest object to access this service, I encounter the Access-Control-Allow-Or ...

Does an equivalent of the JQuery PHP Library exist, utilizing $.ajax in place of $.php?

The JQuery PHP Library introduces an object named $.php, which operates similarly to $.ajax. Here is an example of how it can be used: $.php(url); php.complete = function (){ $('#loading').slideUp('slow'); } While this library enh ...

Generating a curated Youtube playlist automatically

I've come across a list of Youtube links that I want to add to a Youtube playlist. However, there are too many links for me to manually add them one by one. I thought it would be a great opportunity to learn some Javascript tricks while automating the ...

Email form successfully retains session variables but fails to accurately update form inputs upon refresh

After experimenting with ChatGPT to implement an email form, I was pleasantly surprised by its performance. However, I encountered a challenge when I wanted the form inputs to retain the information from the previous user session. While it does display one ...

Enhancing Efficiency with Laravel 5: Updating Multiple Data Entries

My Simple Todo App lacks the ability to persist multiple record updates simultaneously. The client-side version can be found here: http://codepen.io/anon/pen/bVVpyN Whenever I perform an action, I send an HTTP request to my Laravel API for data persistenc ...

Ways to pass JavaScript variables to a Python script

Hey there! I'm currently facing an issue with retrieving variables from JS to use in my python code. Despite trying methods like AJAX and JQuery, I haven't had much success yet. It's possible that I'm missing something crucial in the pr ...

Broadcasting data between controllers in AngularJS allows for seamless communication and

Currently experimenting with a scenario where I log in, and upon successful login, I want to store the LoginID and pass it to all other controllers for navigation and user management. This is similar to the concept of setting up a global variable that can ...