js The style properties function returns an empty value for the CSS styles

Looking to incorporate the CSS properties of a div into a JavaScript conditional statement.

Here is the CSS code snippet:

div.desc{
   position:relative;
   top:0px;
   left:0px;
}

Now, attempting to retrieve and alert the 'top' property using JavaScript.

alert(document.getElementsByClassName('desc')[0].style.top)

However, receiving a blank return. Is there an error in my code? Can I actually access the CSS property of the div with the class 'desc' using JavaScript?

Answer №1

Using jQuery, it is possible to achieve this task.

alert($('.description').eq(0).css('top'));

Answer №2

Is the top style set for the initial element with the class desc? If not, it may be inherited or deduced.

Answer №3

Here is a way to retrieve it

const element = document.querySelector('.description');
const style = window.getComputedStyle(element);
alert(style.marginTop);

See the example here

​​

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

Chrome version 83 encounters an issue preventing file uploads

I recently updated my Chrome browser to version 83 and encountered some issues with certain forms that utilize an "Ajax upload" component. Despite researching the problems associated with this new version (https://developers.google.com/web/updates/2020/05/ ...

Trouble receiving Ajax response

Below is the code snippet I am working on: function f(){ var value = ""; var request1 = $.ajax({ url : '/a', type: "GET" }); var request2 = $.ajax({ url: '/b', type: ...

Are jQuery Accordion and Magento causing issues: Potential clash?

I am currently utilizing the jQuerytools accordion/tab feature within a Magento platform, however I am encountering issues with the script not functioning properly. If you take a look at the functional page linked below, you will see that the content and ...

Exploring further data using Firebase query

When working on my Angular Firebase project, I came across the following issue with data querying: Data: https://i.sstatic.net/UHWut.jpg The data is stored from oldest to newest (1 to 4). To retrieve this data, I used the following code: var startHe ...

In jQuery, apart from utilizing class, id, and text properties, what other methods can be used to pass a variable within an element?

Currently, I am facing a scenario where the class and id attributes are already assigned, but I need to pass a variable to jQuery. How should I go about this? Here is the HTML code: <input type="text" id="cannot_change_this" class="rather_ ...

Comparing values in React Redux state using hooks

Recently, I developed a table component using hooks that makes an API call to the backend every time the page loads. While waiting for a response from the API, a loading spinner is displayed. The state management is handled with redux, so when a response i ...

Struggling with integrating jQuery append into Backbone.js

Having trouble using jQuery.append() and backbonejs. Currently, when attempting to append, nothing happens (except the jQuery object is returned in the immediate window) and the count remains at 0. Manually adding the element has not been successful. I als ...

Specify the height of a div tag based on a certain condition

I'm trying to style a div tag in a specific way: If the content inside the div is less than 100px, I want the div tag's height to be exactly 100px. However, if the content's height exceeds 100px, I want the div tag's height to adjust au ...

I am looking to add color to a particular element in my HTML code

Is it possible to style the button where the number is greater than 25, similar to how we did with numbers greater than 15? Your assistance is greatly appreciated. Thank you. I have been struggling to apply specific styling to a particular item in my HTM ...

Using Python's Requests library to authenticate on a website using an AJAX JSON POST request

I'm a beginner in Python and struggling to create the correct code for using Python requests to log in to a website. Here is the form code from the website: <form autocomplete="off" class="js-loginFormModal"> <input type="hidden ...

Issue with .live function toggle (close) not functioning properly following an ajax call

I am currently facing an issue with my ajax request that is responsible for loading new posts. Each post includes a toggle button that should show and hide a hidden element by default. Although the code provided below is functional, it only works properly ...

Create a webpage in full screen mode with a video player that fills the entire screen

Here is the HTML code I am working with: <div id="container"> <video id="video" src="video.ogv" loop></video> </div> The "container" div and video element occupy the entire screen. #container { po ...

The AngularJS UI-Router transition occurs without updating the URL when parameters are passed

When using UI-Router to navigate to a detailed view, I am facing an issue where the state changes correctly and parameters are passed correctly, but the URL address in the browser remains unchanged. I want to be able to display the passed params in the URL ...

Postponed execution for jQuery Ajax universal handlers

As per the jQuery documentation, global Ajax event handlers must be implemented using callback functions on the document. However, local $.ajax() events can utilize promises instead. Is there a method to handle global Ajax events using promises such as .d ...

Angular dynamically selects a dropdown option when the page is loaded

Check out this dropdown example: <div class="col-md-6"> <div class="form-group> <label class="control-label">Role</label> < ...

Creating unique validations for Angular FormArray (ensuring that the sum of proportions equals 100%)

I implemented a formArray in Angular to manage the distribution of nominees, with fields for Name and Proportion. The plus button (+) adds a new row, while the (X) button deletes the current row. I am now looking to create validation logic that ensures the ...

jQuery accordion section refuses to collapse

In order to achieve the desired outcome, each Section should initially appear in a collapsed state. Panel 0 and 2 start off collapsed, however, panel 1 does not but can be collapsed upon clicking. Additionally, Panel 1 incorporates an iframe that displays ...

I'm curious to know why my jQuery function fadeOut is functioning properly while slice isn't working as expected

I'm trying to create a button that displays three consecutive posts After clicking on "view all", the three "div" elements should become visible I'm attempting to use jQuery to make the three 'div' elements appear when the view all bu ...

Creating a dynamic menu structure by tailoring it to the specific elements found on each page

Currently, I am facing issues while attempting to generate a dynamic menu based on the elements present on the page. Is there a way to develop a menu using the following code structure?: <div class="parent"> <div class="one child" id="first"& ...

Utilizing Javascript to load and parse data retrieved from an HTTP request

Within my application, a server with a rest interface is utilized to manage all database entries. Upon user login, the objective is to load and map all user data from database models to usable models. A key distinction between the two is that database mode ...