When using jquery, the .css("border-color") method fails to provide a return value

I came up with the jquery javascript below


$(document).mousemove(function(event) {
    var element = event.target;
    $(element).css("border","black solid 1px");
});

$(document).mouseout(function(event) {
    var element = event.target;
    console.log( "1 = "$(element).css("border-color") )
    $(element).css("border","0px");
    console.log( "2 = " + $(element).css("border-color") )
});

This code creates a border around hovered elements. Despite this, when I tried running it in Chrome, the

console.log( "1 = "$(element).css("border-color") )
and
console.log( "2 = "$(element).css("border-color") )
outputs nothing instead of black, which I expected. I tested using borderColor as well, but no success.

Despite the lack of output in the console logs, the code successfully draws frames around hovered elements. Why do you think the console is not displaying the expected color value?

Answer №1

For jquery, shorthand cannot be utilized. It is essential to provide more specific instructions, such as "border-top-color."

Please inform me if this solution is effective.

Answer №3

$("#mytext").focus(function () {

  $(this).css({'background-color': '#FFFFCC'});

  $(this).css("border", "black solid 1px"); 

}).blur(function () {

  $(this).css({'background-color': '#fff'});
  $(this).css("border", "#DCDCDC solid 1px");

 });


<input type="text" id="mytext">

Give this a try!

Check it out here!

Answer №4

An inspector can assist you in confirming the absence of a property named 'border-color'. Instead, you should be searching for 'border-[top|bottom|left|right]-color'

Answer №5

When it comes to setting colors using JavaScript/jQuery, it seems like CSS alone won't cut it. Accessing border properties might be a bit tricky with jQuery, but native JavaScript does the job just fine.

console.log(this.style.borderTop); // should return '1px solid rgb(0, 0, 0)'
console.log(this.style.borderColor); // should return 'rgb(0, 0, 0)'

Note:
.border(Top|Right|Bottom|Left) is reliable, but
.border(Style|Color|Width) can yield mixed results.

It seems like the outcome depends on whether the shorthand sides are identical; for example:

  • All sides have the same settings
  • top and bottom (matching each other) / left and right (matching each other)
  • top / left and right (matching each other) / bottom
  • No success when all four sides are set differently
  • I haven't had luck with simply using element.style.border to set the whole border at once (e.g.
    element.style.border = '1px solid #000'
    )

Answer №6

It seems like the reason you are not seeing a border is because in the code snippet you shared, there is no initial border defined. Instead of relying on jQuery to create the border through events, it would be more efficient to first specify a border for the element in your .css file and then use jQuery to adjust it as necessary.

If you prefer not to set a border in your .css file, you can try the following example:

Please note that using .css("border-color") is incorrect syntax; instead, utilize camelCase. For instance:

$("element").css("borderColor", "1px solid black");

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

Customizing an external module class using CSS module pattern in React

Is there a way to locally override an external module's CSS class for a specific component? Here is my current code: import `style` from './style.module.css' // local CSS module import `ExternalComponent` from 'ExternalComponent&apos ...

Guide to modifying the behavior of a dynamic dropdown menu

Jquery: $('body').on('change', '.combo', function() { var selectedValue = $(this).val(); if ($(this).find('option').size() > 2) { var newComboBox = $(this).clone(); var thisComboBoxIndex ...

Tips for manipulating the DOM: Pushing existing elements when new ones are dynamically created with JavaScript

I have a pre-existing DOM element (let's say a div) and I am using JavaScript, specifically React, to create a new element - another div which serves as a sidebar. The goal is for the sidebar to seamlessly shift the previous element without overlappin ...

View is unable to trigger the success attribute

Currently, I am delving deep into Backbone JS and facing an issue where the 'success' function in 'EditUser' is not being triggered. I would greatly appreciate it if someone could guide me on what changes I need to make in the code? B ...

Error encountered during Ajax request - two files being transmitted instead of one

Can someone assist me with a basic ajax call for a login button? I need help with the form submission and sending the request to a php file to handle the login action. However, I am encountering an issue where two files are being sent instead of one when ...

Retrieve variables of chained jQuery selectors in PHP form

As someone new to programming, I'm seeking assistance with navigating a particular issue that has me stumped. Essentially, I am struggling to figure out how to retrieve the selected variables from jquery.chained.remote.js and incorporate them back int ...

Update the parent node in the Google Org Chart

This is my first time working with Google Charts and I have a couple of questions: Is there a method in the API to update the parent of an existing node? I am facing a challenge where I need to build a tree structure top-down, which means that I know the ...

Center alignment is not being applied to the SVG element

I am currently working with React and when my component renders, it displays an SVG inside a div. Here is a snippet of the code: render() { return( <div class="myClass"> <svg> ... </svg> </div> ); ...

What is the best way to combine two sections in html/css/bootstrap?

I've been trying to create a simple webpage with a navigation bar and a section below it, but I keep running into an issue where there's unwanted white space between the nav bar and the next section in blue. Is there a way to eliminate this gap a ...

Prevent the wrapping of table cells in CSS tables

I'm currently developing a board game in JavaScript, and I have stored the images for the board within a table structure. Initially, I used HTML <table> tags for this purpose. However, I later realized that since the table doesn't contain d ...

Retrieve information from a database in real-time using Ajax without needing to set a refresh interval

Currently, I have been working on jquery/ajax requests and have managed to create an ajax request that retrieves data from a database. However, the issue at hand is that I am continuously utilizing window.setInterval() to refresh this function every x seco ...

What causes Firefox's CPU to spike to 100% when a slideshow begins that adjusts the width and left coordinates of certain divs?

Seeking Advice I'm in need of some help with identifying whether the code I'm working on is causing high CPU usage in Firefox or if it's a bug inherent to the browser itself. The situation is getting frustrating, and I've run out of so ...

Creating a Striking Multi-Layered CSS Header

Can someone help me figure out how to add horizontal lines behind the words in this header? Here is what I have so far: https://i.sstatic.net/dN7s4.jpg. However, I want to achieve something similar to this design: https://i.sstatic.net/FZoSF.jpg. You can v ...

How can I create a dropdown menu that is dependent on another dropdown menu using Ajax in my Laravel application?

I have two dropdown fields that are dependent on each other - Class & Section. I am trying to Select * from sections where class_id=selected Class Id. Although I attempted to achieve this using java script, it doesn't seem to work for me. Here are ...

Changing an HTML container using jQuery and Ajax to facilitate a login process

Currently, I am on the lookout for a unique jQuery plugin that can replace a div when a successful login occurs. Despite searching multiple times on Google, I have been unable to find an ideal plugin that meets my specific needs. Do any of you happen to kn ...

Is a non-traditional fading effect possible with scrolling?

I have implemented a code snippet to adjust the opacity of an element based on scrolling: $(window).scroll(function () { this = myfadingdiv var height = $(this).height(); var offset = $(this).offset().top; var opacity = (height - homeTop + ...

How to place a div with 100% width at the bottom of a float with a fixed width

Each day the same question seems to pop up, yet this particular issue has me stumped... My goal is a simple one - I aim to design a fixed width layout with 3 columns, while ensuring that the header and footer stretch across 100% of the screen. Everything ...

Cease animation if the page has already reached its destination

In my current setup, I am using a JavaScript code snippet to navigate users to the specific location of the information they click on in a side navigation menu. However, one issue that arises is if they first click on one item and then another quickly, t ...

The div is incorrect and causing the label and input to move in the wrong direction

Whenever I try to adjust the position of the current-grade-name-input, the entire grade-point-input moves along with it inexplicably. /* final grade calculator: (wanted-grade - (current-grade * (1 - final%))) / final% Ex: have a 80 an ...

How can I prevent text highlighting on a website?

Is there a way to lock the copy button on my site without restricting the save as button, which is activated by right click? I want users to be able to save the website as an HTML file, but prevent them from copying text. Can this be achieved using Javas ...