Retrieve the value of a style property using the css() method

<p class="pen" style="color:#000">abc</p>

.pen{
color:#333
}

In the previous code snippet, we have an HTML element with inline style and a CSS class that defines color properties. Despite the inline style having higher precedence, jQuery returns the color defined in the CSS class instead of the inline style. How can I retrieve the color from the current style using jQuery?

Answer №1

If you have two elements that share the same class name

<p class="pen" style="">abc</p>
<p class="pen" style="color:#000">abc</p>

and then execute the following code:

$('.pen').css('color')

The output would be #333 or rgb(51,51,51), because the selector identifies the first matching element. This is why your code isn't working as expected - there are multiple elements with the class pen.

Check out the demo here


However, if there is only a single pen element or if the order is different like below:

<p class="pen" style="color:#000">abc</p>
<p class="pen" style="">abc</p>

the output will be #000.

Here's another demo for reference

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

There are times when JQuery scripts fail to function correctly

Hello there. I'm having an issue with my website - www.robbiedawson.com. Sometimes, the text in the grey footer fails to load properly, including numbers and links, making navigation impossible. This happens occasionally when refreshing the page, whet ...

A guide on implementing moment.js in EJS views to display the user's local time instead of the server's

I've set up Moment js on my server using app.locals to pass it around. app.locals.moment = require('moment'); Despite trying to call the local() function when showing the time, it continues to display the time based on the server's ti ...

Arrange the array in chronological order based on the month and year

I'm looking for assistance with sorting an array by month and year to display on a chart in the correct order. Array1: ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'....]; Desired Output: ...

What is the best way to showcase a set of paired arrays as key-value pairs?

Currently, I am developing a client in React that is responsible for receiving streaming data that represents objects from the back end. The client's task is to parse this data and dynamically construct the object as a JavaScript data structure, typic ...

Issues with Image and Product Name Rendering in Outlook for Windows

Having a bit of trouble with Outlook PC not cooperating with my product images and names. While other email clients show the product names below the images as intended, Outlook PC is causing the names to appear next to the images like this - Product name ...

Axios - Error: Promise Rejected - The request was unsuccessful with a 500 status code

The Axios post request in my code for adding new articles is not going through, and I am encountering an error: Failed to load resource: the server responded with a status of 500 (Internal Server Error) createError.js:17 Uncaught (in promise) Error: Requ ...

Troubleshooting Problem with Bootstrap 4 Navigation Bar Dropdown Feature

I attempted to divide the dropdown menu in the navigation bar into two columns, but they are overlapping with each other. Another issue I noticed is that the Search bar remains at the bottom on mobile devices and the page does not respond properly. I am u ...

Retrieve GPS data source details using Angular 2

In my Angular 2 application, I am looking to access the GPS location of the device. While I am aware that I can utilize window.geolocation.watchposition() to receive updates on the GPS position, I need a way to distinguish the source of this information. ...

Enhance the brightness and add a subtle glow to an image inside a div element when hovering over the div

Is there a way to create an effect where the image within a div brightens when hovering over the entire div without affecting any other elements? Here is my code, any suggestions or improvements are appreciated. #textimg{ background-image: url( ...

The Ionic2 http post request is missing the 'Access-Control-Allow-Origin' header

Here is the complete code snippet: this.http.post(link, data, { headers: headers }) .map(res => res.json()) .subscribe(data => { this.data.response = data._body; }, error => { console.log("Oops! An error occurred"); ...

Using jQuery to send an Ajax request to a Web Method

I am encountering an issue when trying to send data to my code behind method. Everything works perfectly fine until I add something to the data parameter. function (obj) { $.ajax({ type: "POST", url: "Pages.aspx/EditPage", data: "{ ...

The Ultimate Slider: Highlighting Custom Navigation Link as Active While Navigating with Arrows

I have implemented custom navigation links for my slick slider in order to navigate to specific slides. The functionality works perfectly, but I encountered an issue when I added the built-in arrows provided by the slider. Whenever I use these arrows to n ...

fullpage.js: the content exceeds the height limit

I am currently working on customizing the jquery script fullpage.js for a website built on the French CMS "SPIP" (). This script is used to create a one-page website with both horizontal and vertical navigation. However, I have encountered an issue with ...

Exploring VueJS: How to retrieve a specific value from an array within JSON data obtained from an API request?

Currently, I am creating a blog completely from scratch with both front and backend components that communicate through an API I developed. Below is the JSON output of the API: { "status": true, "posts": [ { ...

Is there a method to retrieve all Class elements without using a for-loop?

I am trying to retrieve all elements with a specific class using document.getElementsByClassName(); <body> <div class="circle" id="red"></div> <div class="circle" id="blue"></div> <div class="circle" id="yell ...

Passing a value back to a template from a promise

Currently, I am implementing server-side validation for dynamically created input fields within div tags. The issue I am facing is that I need to display the API error message in my template instead of directly setting it in my controller. If I set it in t ...

Deactivate validations on bootstrap-4 form upon submission

Even after successful submission, the validations are still visible. I have attempted to clear all input fields and resubmit, but the validation icons persist for each field. $('#formid').on('submit', function(e) { // if the validat ...

Modify JSON date format to a shorter version using the first 2 letters of the month and the year

Looking to convert date values in a JSON array from "December 2016" format to "D16". Hoping to accomplish this using Regex, any assistance would be greatly appreciated. [["November 2016","December 2016","January 2017","February 2017","March 2017"],["tot ...

"Effortless technique for calculating the total of a column within a

Below is the code snippet for my repeater: <asp:Repeater runat="server" ID="repBasket"> <ItemTemplate> <li> <a href="http://stylo.senseithemes.com/?product=flying-ninja"> <img width="100" height="130" sr ...

Tips for efficiently merging various axios responses

I'm currently working on a React application where I need to create two objects using data from three different APIs. To illustrate: DataObject1 will be generated using API1 and API2. DataObject2 will be generated using API1, API2, and API3. As I c ...