Using jQuery to alter the attributes of a hover class

Is there a way to modify the Hover-Background-Color?

   <style>
      .myhoverclass {background:#FFF;}
   </style>

I am using this code to toggle:

   $('.box a').hover(
        function(){ $(this).toggleClass('myhoverclass') }
   )

However, when I try to change .myhoverclass with ColorPicker...

  $('#li-a-bgcolor').ColorPicker({
    onChange: function (hsb, hex, rgb) {
        $('#li-a-bgcolor').css('backgroundColor', '#' + hex);
        $('.myhoverclass').css('background-color', '#' + hex);
    }
  });

...the color stays white. Can you point out where the error might be?

Appreciate your help.

Answer №1

It seems like you are trying to choose a color and change the hover color, but modifying the class definition/rule for .myhoverclass {background:#FFF;} can be quite challenging.

Instead of that, I recommend considering a solution similar to this: http://jsfiddle.net/k6kp1e5n/

HTML

<div id="colorPicker">
     <div style="background-color: #0000FF"></div>
     <input type="text" value="#0000FF">
</div>
<br><br>
<div class="box">
    <a href="#">Hover me</a>
</div>

JS

$('#colorPicker').ColorPicker({
    color: '#0000FF',
    onChange: function(hsb, hex, rgb) {
        $('#colorPicker div').css('backgroundColor', '#' + hex);
        $('#colorPicker input').val('#' + hex);
    }
});

// Managing "hover"
$('.box a').on('mouseenter', function (e) {
    $(this).css('background-color', $('#colorPicker input').val());
});
$('.box a').on('mouseout', function(e){
   $(this).css('background-color','#FFF');
});

CSS

.box {background:#FFF;}

Answer №2

Keep it straightforward. Develop two separate classes with specific colors assigned to each. Simply toggle between the classes when hovering and moving your mouse away.

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

Exploring the Power of jQuery Ajax Requests

Currently, I am making an Ajax call from site.com/users/{username} I need to access the url site.com/account/deleteComment, but when I check in fireBug it seems to be trying to access site.com/users/account/deleteComment Below is the code snippet: $ ...

Creating a form that can identify both letters and numbers using JavaScript

Is it possible to create an <input> field that can recognize both letters and numbers while disregarding spaces and capitalization? Here is my current progress, aiming for the feedback to display as "right" when 9 H 6 U 8 f is entered in the field, ...

jQuery.appear is seemingly malfunctioning

Does anyone know why this specific HTML code isn't functioning correctly? The intention is for an alert to appear when the image becomes visible on the screen and then again when it disappears from view. Even when utilizing local libraries, this scr ...

Enhancing Data Tables with Jquery: Implementing Column Filtering for Multiple Elements within Table Cells

I am looking to utilize jQuery datatables for filtering a column that may contain multiple values within a single td. To achieve this, I referenced the example provided in the datatables documentation. In the image below, my goal is to filter the office ...

Guide to match the size of <td> with an <img>

I am currently attempting to align several images in my HTML using the <table> element. My goal is to eliminate any white space between the images. You can view my progress so far in this JSFiddle example: JSFiddle example. However, I am still encoun ...

Create a stylish design with Bootstrap 3.0 featuring a full-width color background and compact columns perfectly centered on the

As I ventured into creating a business theme with stripes, akin to the one developed by W3Schools and accessible here, I encountered a particular challenge. The layout consisted of horizontal sections with varying background colors. My main concern was th ...

Creating a button that integrates an image within a single div element

Currently, I understand the code: $('#leftDev').css("background-image", "url(img/1.png) "); will set the entire background of my leftDiv to display "1.png". However, I would like to position this image as a regular picture within the div and no ...

Display a modal dialog showcasing the progress while making an ajax request

I am working with an Excel sheet that contains over 30,000 records. My goal is to insert each record into a database and display a modal window with a progress bar showing the percentage of completion along with details such as total number of records, num ...

CKFinder Error: Unable to find definition for t.event.special.swipe

Upon using CKFinder 3.2.0 with Firefox 44.0, I encountered the following error message. Interestingly, this issue is exclusive to Firefox while Chrome works perfectly fine. Any insights on what could be causing this problem and how it can be resolved? Ty ...

The jQuery function $.post fails to include information about which button was clicked in the request

I've encountered a problem where $.post isn't sending information about which button was pressed in my form. The scenario is having a form with two buttons: <form method="post" action="/home/index"> <input type='hidden' n ...

The JQuery click() function may fire twice if triggered by the live() method

http://pastebin.com/1MGTtzij is where you can find my complete HTML document. Here's a condensed version: $(document).ready(function () { $("#myDiv").append($("<input />", { type : "button", ...

Using a loop to format the <ol> tag

After creating an HTML script using angular, I was able to successfully display the names in an array as shown below. <div ng-repeat="namep in names"> <li>Name : <span ng-bind="namep.name"></span></li> <li>Age : ...

Having trouble making class changes with ng-class

I am attempting to change the direction of the arrow in my checkbox by utilizing two classes with the assistance of ng-class. Unfortunately, it is not producing the desired outcome. Below is my code: Note: The angularJS CDN has already been incorporated. ...

Vuetify's scrolling list feature allows for smooth navigation through a list

Check out my Vuetify code snippet that utilizes a list: <v-list> <v-list-tile v-for="user in users" :key="user.id" avatar @click="" > <v-list-tile-content> < ...

Adjust the vertical scaling of a container in CSS Grid to maintain aspect ratio similar to an <img>

I am attempting to recreate a demonstration where the house image scales as the viewport height decreases. However, instead of using an <img>, I would like to use a container that also includes absolutely positioned content (unable to provide all the ...

The issue in jQuery arises from a syntax error within an array

Seeking assistance to identify the issue in the following code snippet: function cat_autocomplete(){ var categoryTags = ["boots>black","boots>brown>boys>blue","clothes>small_men>scarfs>blue","boots","boots>brown>boys","boots ...

The issue arises when trying to implement CSS keyframes with a CSS image slideshow

I'm currently troubleshooting a CSS slideshow that isn't functioning properly. The issue lies within the keyframes. I'm attempting to make the images come down from the top mask and then have the previous image go back up into the mask. This ...

Creating an adjustable fixed footer using Bootstrap 3

Struggling with the application of bootstrap styling. I have a footer with columns set as col-md-3, 3, 2, 4 which sums up to a total of 12. The issue lies in the differing content within each column. My goal is to achieve equal spacing between each div in ...

Having trouble retrieving data from a remote URL with angucomplete-alt

Utilizing the angucomplete-alt directive for creating an autocomplete list through a get request. Successfully fetching results from the Github API: <div angucomplete-alt id="ex5" placeholder="Search projects" pause="500" selected-object="selectedProj ...

Reimagining scrolling through content with a stylish unordered list (a sleek alternative to a select box

After having our company website redesigned by a professional designer, our site now looks much more visually appealing. However, I have encountered difficulties when trying to implement their design using HTML and CSS. One particular challenge is the heav ...