What might be causing the issue with my jQuery hover function not working as expected?

I'm having trouble with defining a hover function for the buttons I created. It seems like the if/else condition is not working properly. What I want to achieve is changing the background color of my button when hovering based on a specific condition. However, the function seems to be skipping the IF statement and only executing the ELSE block, regardless of whether the condition is met or not.

function clickHTML() {
  $("#html").css("background-color", "#F5FEFE");
}


$(".button").hover(function() {

  if ($(this).css("background-color") == "#F5FEFE") {

    $(this).css("background-color", "#AFF7F7");
    console.log('inside the if branch; color changed');

  } else {

    //alert($(this).css("background-color"));
    $(this).css("background-color", "#E4E4E4");
  }

}, function() {

  $(this).css("background-color", "#EDEDED");
  $("#html").css("background-color", "#F5FEFE");
});
h3,
html,
body {
  font-family: Helvetica, sans-serif;
  margin: 0px;
  padding: 0px;
}

#header-bar {
  width: 100%;
  height: 50px;
  background-color: #EDEDED;
}

h3 {
  padding: 14px 400px 0px 5px;
  float: left;
}

.button-group button {
  width: 100px;
  background-color: #EDEDED;
  margin: 10px 0;
  border: 1px solid black;
  color: black;
  padding: 5px;
  display: inline-block;
  text-align: center;
  text-decoration: none;
  font-size: 12px;
  float: left;
  border-radius: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet">

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js">
</script>

<body onload="clickHTML()">

  <div id="header-bar">

    <h3> CodePlayer </h3>

    <div class="button-group">

      <button id="html" class="button"> HTML </button>
      <button id="CSS" class="button"> CSS </button>
      <button id="js" class="button"> JavaScript </button>
      <button id="output" class="button"> Output </button>

    </div>
  </div>
</body>

Answer №1

When you use console.log to display the value of

.css('backgroud-color')

You'll notice that it's in RGB format, not hex. If you need to compare it with a hex value, you can convert it first using this function:

var rgbToHex = function (rgb) { 
  var hex = Number(rgb).toString(16);
  if (hex.length < 2) {
       hex = "0" + hex;
  }
  return hex;
};

Answer №2

It is recommended that you utilize

if ($(this).css("background-color") == "rgb(245, 254, 254)") {

in place of

if ($(this).css("background-color") == "#F5FEFE") {

Furthermore, for all other colors, consider using RGB format instead of 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

Disabling the outline border on bootstrap select elements

I have integrated this plugin into my project and I am attempting to eliminate the blue border when the select box is focused. I attempted to achieve this by setting the outline to none using the following code: *:focus { outline: 0!important; } Additi ...

Positioning a single column of iframes in the center

I have 6 graphs that I need to arrange in a grid with 2 rows and 3 columns. Each row should have one left-aligned graph, one center-aligned graph, and one right-aligned graph. However, the center-aligned graphs are not displaying correctly. Can someone hel ...

changes in size in relation to the position of the parent element

Utilizing the transition:scale(1.2) property to conceal a div positioned in the lower left corner of the viewport. Currently, the scaling effect originates from the center as intended: View 'CURRENTLY' on Fiddle I aim to scale it as if the div ...

A pale tooltip with a light arrow appearing against a soft white backdrop

Can someone help me figure out how to display a white tooltip with a white arrow? I've tried to implement it using the code below, but the white color is not visible against the background. Any suggestions on making it stand out? https://i.sstatic.ne ...

Application that utilizes Django to determine the location of a mobile device user

I'm looking to develop a mobile-responsive web application using Django-Python that will primarily be accessed on smartphones. The main functionality I want to incorporate is the ability for the app to track the user's location and display it on ...

Download the ultimate HTML package with a built-in chart.js component directly from your nuxt app

I have a task to create a compact Nuxt application that produces a basic HTML file containing informative sections about the services offered to the clients of my organization. The main purpose is to generate an HTML file that can be easily downloaded and ...

What is the best way to implement a collapsible menu in a fixed sidebar for mobile devices using Bootstrap 4?

I have been searching extensively, but I have yet to come across the precise solution for what I am trying to achieve. Being new to coding, I believe that the answer is probably simpler than I realize. My goal is to develop a sidebar navigation that remai ...

cannot send a null value within a JSON payload

JQuery: var array = new Array(); var selectedValue = null; if ($('#chk').is(':checked')) { var list = $('#sellist option:selected'); $.each(list, function(index, item) { ...

What is the best method for inserting CSS into a webpage using a Chrome extension?

I am seeking guidance on how to incorporate CSS into a webpage using a Chrome extension. The CSS code I am attempting to inject is as follows: body { background: #000 !important; } a { color: #777 !important; } Here is the content of my manifest.j ...

The Twitter Bootstrap modal pops up over the menu, making the links unclickable

Currently, I am employed at a website called . If you navigate to the "Serviços" section on the homepage, you will encounter a row of icons and a blue button labeled "saber mais". Clicking on this button triggers a modal box that displays additional infor ...

Is the functionality of the jquery trigger('resize') limited to only one instance?

Here are two code snippets that I have: function adjustFooter() { var wrapper = $('#disqus_wrapper').height(); if ( wrapper > 200 ) { $(window).trigger('resize'); } }; var element = $('#disqus_wrapper&apos ...

What is the best way to "fold" a div from the bottom upwards?

I'm looking to achieve a specific layout change when checking a checkbox, but I've run into an issue. Currently, the top portion of the content folds up while the bottom remains static. However, my goal is to have the opposite effect where the to ...

When doctype is specified, `$(window).height()` will output the height of the document

Recently, I've encountered an issue where trying to retrieve the browser's window height using $(window).height() has been returning a significantly larger number, likely indicating the document height instead. It's strange because I've ...

Using jQuery to retrieve a file that has been uploaded through an input field with the type of 'file'

I am looking to extract the file that has been uploaded using an <input type='file'> tag. Currently, when I use $('#inputId').val(), it only retrieves the name of the file, not the actual content of the file itself. I came acros ...

"The Jquery Ajax Each function is designed to give back twice as much

I am currently facing an issue with a Jquery Function that is responsible for fetching a list of users from the database and displaying their information in div elements. The problem I am encountering is that I am getting duplicate results. This is my firs ...

Double Looping of Ajax on Shopify Order Form

I have an Ajax order form on a product page. Every time the #submit-table button is clicked, it should display a drop-down menu with updated cart information, such as quantities and prices, along with the newly added products. Here's an example of th ...

API and PHP Combination for Efficient Insertion and Updating

I am facing a problem with my product editing feature and adding a new language in my current API. If there is already a language present, I can display and update it without any issue. However, the problem arises when there are no additional details avail ...

Using a single name in the form, input multiple parameters and retrieve a comprehensive list of all of them

I'm struggling with a form that has dynamic fields. After submitting the form, I am getting this URL: Localhost:8000/mysite.com/jobs_form?job="Job1"&job="Job2" But I am unable to retrieve all of them in Django using reques ...

Receive information on browser activity

Is there a way to trigger an event when the following actions occur: Pressing the reload icon in the browser Pressing the Back or Forward icon in the browser Selecting the Reload menu item from the browser context menu Selecting the Reload option from t ...

Organizer featuring Outlook-inspired capabilities

My goal is to develop a system for efficiently managing appointments. Upon opening the application, I want to display a calendar control that will retrieve an individual's schedule from a SQL server database. For example, if the user has meetings sch ...