Is it possible to access the CSS property from an external CSS file even when inline styles are used?

Is there a way to retrieve the css property from an external source using JavaScript or jQuery even if inline styles are applied to the same element? Here's an example:

<div id="my" style='left:100px'>some content</div>
<style>
    #my{left:250px;}
</style>

How can I get the value of 250px without removing the inline style?

Answer №1

Is it possible for you to make changes to the markup once the page is fully loaded? If that's the case, I recommend using jQuery to set the style attribute to an empty value and then retrieve the left position using jQuery.

var my = $('#my');
my.attr('style','');
var position = my.position();
alert(position.left);

Alternatively, if you prefer not to cache the variable:

$('#my').attr('style','');
alert($('#my').position().left);

Answer №2

In my opinion, the most effective approach is to temporarily remove the inline style, analyze it, and then reapply it.

Removing the style may not result in any visible changes to the element because the process happens quickly.

http://jsfiddle.net/abc123/

function examineStyle() {
    var element = document.getElementById('myElement');
    var attribute = element.getAttribute('style');
    element.setAttribute('style', '');
    var value = element.offsetLeft;
    element.setAttribute('style', attribute);
    return value;
}​

Answer №3

To achieve this, you can opt to fetch the .css file using AJAX (for example, with the convenient jQuery method $.get()), and then analyze it by utilizing a tool like JSCSPP or an alternative parser (such as another jQuery-based parser). Although some may view this approach as excessive, it offers the benefit of enabling access to all the rules following a single retrieval operation.

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

Utilizing Jquery to retrieve an array as the return value from a $.post request

After going through numerous discussions on this topic, I'm still struggling to make it work. However, I've made significant progress. I have a scenario where I send data from jQuery to my database. The database returns a record consisting of two ...

How to Use Laravel to Create HTML Divs Without Images

While working with a text editor, we have the ability to incorporate various HTML tags such as images and videos. However, I am only interested in displaying text fields. When I use {{$loop->content}}, it displays: <p><strong>EXAMPLE</st ...

Can display-inline-block support top margin and text alignment?

Can you please explain if the top margin and text-align properties work for both display: inline and display:inline-block? If they do, could you provide some insight as to why? ...

jQuery does not support iterating over JavaScript objects

Here is the code snippet I am working with: $(obj).each(function(i, prop) { tr.append('<td>'+ i +'</td>' + '<td>'+ prop +'</td>'); }); Intriguingly, the data in $(obj) appears as: Obje ...

CSS behaving strangely with the height property

I've run into a specific issue with a div element that contains multiple buttons. I am trying to adjust the height and position of one button so that it takes up space above the other buttons, but every time I make changes to its height and position, ...

Enhanced Organic Draping/Drifting

Currently working on creating a basic thumbnail gallery where clicking on a thumbnail expands it to one of three sizes (square, vertical, or horizontal). However, facing the issue where expanded pictures maintain their original order instead of filling emp ...

Different Line Thickness in Dropdown Menu

Is there a way to adjust line heights in CSS within a drop down menu? I have attempted to change the font sizes of each element, but the new font size does not seem to take effect once an option is selected. Any suggestions would be greatly appreciated! & ...

Is it a poor practice to combine jQuery's $.each with an AJAX request?

Here is the code snippet that I am working with: $(document).ready(function() { var url = "https://graph.facebook.com/search?q=cinema&type=post"; $.ajax({ type: "POST", url: url, dataType: "jsonp", success: fun ...

Is there any benefit to making the SVG elements width and height 100%?

The Angular Material documentation app features an SVG Viewer that is able to scale the SVG content to fit the container using the following function: inlineSvgContent(template) { this.elementRef.nativeElement.innerHTML = template; if (this.sca ...

Interactive font-awesome icons within an interactive dropdown menu

I am currently facing an issue with using two Fontawesome icons in a clickable dropdown menu. The dropdown menu does not toggle when I click on the icons directly; however, it works when I click on the padding around them. The example provided by W3schools ...

How do I reference this span object using jQuery?

My HTML code includes the following: <span id="enterStreamName" style="display:none"> <input type="text" id='Stream' class="fullRowTextBox" value="" name="Stream" /> <button class="SaveStreamButton">Save</butto ...

What could be causing the span class data to not be retrieved by the getText method

Looking to retrieve the value of a span class called 'spacer-right big project-card-measure-secondary-info' <span class="spacer-right big project-card-measure-secondary-info">1</span> snippet of code: browser.waitForEl ...

The use of select2 on an <input> element does not initiate any ajax requests

While using the select tag with ajax functionality, everything runs smoothly. However, when attempting to incorporate select2 into an input tag, the ajax feature is not triggered. Here are three instances demonstrating the usage of select2: <select&g ...

What provides greater performance benefits?

When considering performance, which option is more advantageous? 1) Loading jQuery externally or 2) Hosting jQuery on our server ...

What could be causing the .hover function to malfunction and how can I make it so that the .hover function only applies within the corner radius area?

I am attempting to make circles react to my jquery .hover function. Below is the JavaScript code I am using: jQuery.fn.center = function () { this.css("position","absolute"); this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) + ...

Adding individual buttons at the bottom of each data row using Jquery: A step-by-step guide

Currently, I am receiving data from a backend using an AJAX GET method and displaying it in a list in HTML. However, I am facing some issues with including buttons within the list and making them functional by utilizing delegate and other methods. I would ...

Prevent form submission when processing is in progress

I've got this code working perfectly: $(function() { $("input,textarea").jqBootstrapValidation({ preventSubmit: true, submitError: function($form, event, errors) { // additional error messages or events ...

Items vanish when hovering over them

Creating nested links in the sidebar navigation bar with CSS has been a challenge for me. While hovering over main links, I can see the sub-links being displayed. However, when I try to hover/click on the children of the sub-links, they disappear. Note: M ...

What is the procedure for assigning an element's background-color to match its class name?

Is there a way to use jQuery to make the background color of a span element match its class? $(function() { $("span").css("background-color") }); span { display: inline-block; width: 5px; height: 5px; border: solid #0a0a0a 1px; } <script src= ...

Why is the radio button getting bound to the error instead of the label?

I'm currently working on validating radio buttons in a GSP file using the jQuery validation plugin. The issue I'm facing is that the error message is being bound to the first radio button and appearing at the bottom of it. Instead of displaying ...