Distinguishing Between CSS Classes and Inline Styling in JQuery

Consider the following scenario with two div elements:

.example{
  width: 200px;
  height: 200px;
  z-index: 4;
}
<div id="first" style="height:100px;width:200px;z-index:5;">Hardcoded</div>
<div id="second" class="example" >Css</div>

How can I determine using jQuery or JavaScript if, for instance, the height of a div is defined by the style attribute? Or how can I identify if the height is set, but not through a CSS class?

It appears there may be some confusion about my query. Although one answer came close but was later removed: The question isn't about "what value is assigned to the height". or "whether a specific class is applied" Given a particular style property such as 'height', I am interested in finding out two things:

  1. Has the height been set in any way other than the default browser settings, like via a CSS class or inline style? (I believe I can figure this out on my own)
  2. If so, has the height been defined through the style="...." attribute or via a class=""? The specific class used doesn't matter.

Answer №1

To incorporate jQuery into your code, you can utilize the following example:

<script>
$(document).ready(function() {
    if($('#first').attr("style").indexOf("height") != -1) {
        alert("The height is set inline in this element.");
     }
    else {
        alert("The height is not hardcoded in this element.");  
     }

  });
</script>

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

Setting the CSS position to fixed for a dynamically generated canvas using JavaScript

My goal is to fix the position style of the canvas using JavaScript in order to eliminate scroll bars. Interestingly, I can easily change the style using Chrome Inspector with no issues, but when it comes to implementing it through JS, I face difficulties. ...

Incorporating personalized CSS styles into a Bootstrap 4 card

I have been scouring through countless questions and solutions on StackOverflow, but nothing seems to be working for me. While p-5 is doing its job just fine for padding, it applies to all devices. I am in need of specific CSS for mobile devices when it co ...

Floating label hides the textbox text with a unique style

I'm trying to create a floating label effect for textboxes in my form that moves up on hover and focus. However, I'm running into an issue where the text gets hidden when the label moves up. Below is the HTML and CSS code I've been working ...

Different techniques for retrieving elements generated by ng-repeat from their containing parent

Let's keep it simple - imagine we have a directive called headSlides. This directive's template includes an image that is being repeated using ng-repeat: <img class="bg" ng-repeat="image in images" ng-src="{{image.src}}"> I need to access ...

What strategies can I use to reduce the amount of event listeners assigned to buttons in jquery?

Currently, I am utilizing jquery 1.6.2 for my project. On one of the pages, I have a structure that resembles the following: <div id="section1"> <fieldset> <ul> <li> <input type="radio" name ...

The clone() function in jQuery causes the properties of the original element to become distorted

Seeking help with a curious issue I'm encountering. After selecting a radio button and cloning it, the original radio button becomes unchecked while the cloned one behaves as expected. Any insights into why this might be happening would be greatly app ...

Customizing the Background of an Input Box

How can I create an input box with a specific background? I have tried the following code but it's not displaying properly. What is the correct way to achieve this? In addition, I need to create variations of this button as described below: I wa ...

I am not encountering any error messages, but when I attempt to click on the form, the mouse cursor does not change to the expected form type

While there are no errors showing up in the Chrome Developers Error code, I am facing an issue with my form where the text fields are not visible. It should be a simple task, but it has turned into a headache for me. I would greatly appreciate any help i ...

How to properly size a child div inside a parent container

I'm having trouble with sizing a child div inside a parent div. The problem is that the child div's size changes according to the number of elements it contains, but I want all the child divs to be the same size regardless. This issue arises with ...

Looking to design this chart using CSS and HTML?

Could someone please help me with resizing the circle border in this code? I am facing difficulties adjusting the size and would appreciate any assistance. By running the code snippet, you can see the issue I am encountering and how it differs from the pro ...

Achieving auto-height for two elements with CSS within a single block

I am attempting to combine two blocks into one fixed-height block in order to achieve the following design: ------------------------ UL (starts with height=0), expands when elements are added until maximum height is reached scroll bar should appear aft ...

What is the purpose of the `hidden` class for the `<hr>` element?

I've been tasked with updating a webpage that was passed down to me. One thing I'm working on is adding a print.css file, as it didn't have one before. The original developer included a... <hr class="hidden" /> The main css file has ...

"Troubleshooting JSON parsing in jQuery: one code successfully parsing data, the

These are the snippets of code I have for parsing a JSON response from the server, along with the corresponding responses. The first code seems to be functioning correctly, however, the second code is not producing the desired result. Can you provide any ...

Tips for adjusting the size of iframe content to fit its div area in Bootstrap 4 when the window is minimized

I recently started building a website and encountered an issue with resizing the window to fit an iframe containing a video within a div. I'm unsure where exactly in the CSS code I need to make adjustments for this. Can someone help me out? The websi ...

Is it possible to utilize LESS to dynamically read the width?

My LESS code looks like this: &.settings-prompt .panel { margin-left: -190px; width: 380px; height: 200px; } &.create-playlist-prompt .panel { margin-left: -165px; width: 330px; height: 180px; } I want the .panel to have ...

Live search feature for multiple input fields using jQuery for an array of elements

I am currently trying to implement a jQuery live search feature for multiple input fields. The live search works perfectly fine with just one input field (without an array), but when using multiple search fields with an array, nothing seems to happen. ...

Unleash the power of jQuery.keypress for lightning-fast text processing

Looking to customize the keypress event for real-time text transformation as you type. Here's what I've tried: field.keypress(function(e){ field.val(function(i, val){ return val.toUpperCase(); }); return false; }); The input ...

As I traverse along a row and delete a cell that matches - the 'replace' method is not available

My goal is to loop through a set of td elements within a tr and remove the first one that contains a specific string (each cell holds a word and there will only be one match). The issue lies in this block of code: $('#row > td').each(function ...

Scrollable horizontal card list using Bootstrap

Looking to design a unique layout featuring a horizontal list of cards where 3 cards are displayed simultaneously, with the ability to horizontally scroll through the rest, like this: https://i.sstatic.net/KgX27.png While achieving this using CSS is stra ...

Toggle sidebar: expand/collapse

Looking for some assistance with my website project. I currently have two arrows to open and close the sidebar, but I would like one button to handle both actions instead. Can someone help me edit my code snippet to achieve this? Keep in mind that I am new ...