Discovering the exact distance between a 'li' and a 'div' element

Currently, I am in the process of converting HTML to JSON.

Here is an example of the HTML I am working with:

<div class="treeprofit" id="divTreeViewIncomeDetails" style="height: auto;">
        <li><span class="fa fa-folder-open highlight" id="3" onmouseover="visibleLink('3', 'Incomes', '0')" onmouseout="hideLink('3')"><span onclick="GetTreeViewChartOfAccountsByParam('3', 'Incomes')">Incomes </span><span class="closingbalance">INR 50.00Dr </span></span>
            <ul>
                <ul>
                    ... (content continues)
                </ul>
            </ul>
        </li>
    </div>

CSS:

.treeprofit {
    padding: 10px;
    font-size: 16px;
    border:1px solid #999;
}

    .treeprofit li span :hover {
        background-color: whitesmoke;
        border: 0px;
        color: black;
    }

    .treeprofit ul { 
        list-style-type: none; 
        padding-left:15px;
    }

     .treeprofit li { list-style-type: none; 

     }

        .treeprofit li :hover {

            cursor:pointer;
        }

I am attempting to determine the distance between the li and the div.

I have tried the following code without success:

var items = [];
            var inputData = $('#divTreeViewIncomeDetails').find('li > span');

            for (var i = 0; i < inputData.length; i++) {
                var position, data1, data2;
                position = inputData[i].className;
                data1 = inputData[i].children['0'].innerText.trim();
                data2 = inputData[i].children['1'].innerText.trim();
                var width = inputData[i].offsetLeft;
                var item = { position: position, data1: data1, data2: data2, width:width }
                items.push(item);
            }

Although I am retrieving a value for width, it appears to be incorrect.

Please note: No modifications are to be made to the HTML structure itself.

Answer №1

By employing this code snippet, you can effectively target and take control of every LI element present on the web page. Iterate through them and subsequently output their left offset values to the console.

$('li').each(function(){
  console.log($(this).prop('offsetLeft'))
})

Answer №2

Insert the following jquery code inside $(document).ready(function(){})

jQuery

$("li").each(function(){
    alert($(this).text() + " is positioned at a distance of " + $(this).offset().left + "px.");
});

Take a look at this DEMO.

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

What is the reason that certain CSS pages are not being utilized on my website?

I am facing issues with rendering my website on a tablet device, specifically with my last 2 css pages (800px.css and 800pxland.css). Despite working fine just hours ago, now they seem to be neglected while all the other ones work perfectly. I have tried ...

I need help getting my Vue.JS project to function properly when it is deployed on a server

After creating a new VueJS project using the Vue UI, I noticed that everything runs smoothly locally at http://localhost:8080/ with no errors. However, when I build the project and upload the files from the dist folder to my hosting package via FTP, I end ...

Can loading data after the initial page load improve the performance of web page loading?

Is it beneficial to load non-essential data using AJAX immediately after the page loads in order to speed up overall webpage loading time? There are certain modules on my website that are not crucial, so I am currently waiting for the basic content to loa ...

Output of ngResource compilation

Is there a way to retrieve a single result from my array $scope.trailers? I am encountering an issue where accessing the first index using $scope.trailers[0] returns undefined. The API call is made using ngResource. function getTrailers(pageNo){ ...

Vibrant array of colors in AmCharts' line graphs

I'm looking to enhance my graph by having lines between the bullets in different colors based on their direction. For instance, if a line is rising (going from a bullet of smaller value to greater value), it should be green; if a line is falling, it s ...

Brainstorm: Creative ways to showcase excess content within a div

I'm struggling to find a suitable title for this issue. Essentially, I have a box-shaped div that expands into a rectangle and reveals content when clicked. The problem arises when I animate the div's width during expansion. Instead of positioni ...

Increasing the nth-child Selector in Jquery

Referring to this I am looking to cycle through the nth-child selector, which involves: var i = 1; var tmp = $(this).find('td:nth-child(i+1)'); // I wonder how this can be achieved i++; I have rows with dynamically generated columns i ...

The process of uploading images to a server by making an AJAX call to a PHP file

I have an input file and I want to upload the attached file to the server with a message saying "uploaded successfully" when I click on the upload button. However, I am getting a "file not sent" message. (The uploaded images need to be saved in the uploa ...

The selected text on the webpage is not showing up highlighted

Encountering an unusual issue that is challenging to comprehend and difficult to find a solution for using Google search. I have created a website with multiple web pages. Each page follows the same format: header with links at the top, content area in th ...

Verifying file types with HTML5 drag and drop feature

Is it possible to change the drop zone's background color to green or red based on whether the dragged payload contains supported file types (JPEG)? Do Gecko and Webkit browsers have the ability to determine the file type of drag and drop files? ...

Partially Assessed Ajax Response

I am struggling with my ajax response as it seems to evaluate only some of the html, but not all of it. For instance, I have this code that replaces a div with the response from the request: eval(document.getElementById("test").innerHTML-xmlhttp.response ...

Arranging data using jqGrid's Inline Editing Feature

Can the data on jqGrid be sorted directly on the client side while using inline editing? It appears that the data is not being sorted when the row is editable, even if the row header is clicked. ...

I've been struggling with this javascript error for three days, how can I finally resolve it?

Currently developing a website using react js, but encountering an error every time I try to push to my github repository. Run npm run lint npm run lint shell: /bin/bash -e {0} <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail= ...

Enhancing Image Quality in Microsoft Edge

I'm trying to figure out how to make an image scale with bicubic in MS Edge. Is there a CSS solution or something similar that can change this behavior? Check out this page: On the right side of the page, there are two images with textured backgroun ...

Tips for sending a set to a directive in angular.js?

Forgive me for my confusion. I am passing the name of a collection to my directive: <ul tag-it tag-src="preview_data.preview.extract.keywords"><li>Tag 1</li><li>Tag 2</li></ul> This is how the directive is defined: a ...

Conceal the Slider until Fully Loaded with Slick Slider by Ken Wheeler

I am currently using a slider function called Slick by Ken Wheeler. It is currently being loaded in the footer with just two variables. $('.slickSlider').slick({ dots: true, fade: true }); Currently, ...

Add to an array the recently created span element which was inputted through text in AngularJS

Having some difficulty controlling an array object with a list of span values using a watcher in Angularjs. The current setup works partially - when I input span elements, an array is automatically created for each span. When I remove a span element, the ...

Using jQuery to dynamically add one of two fields to a form

After successfully using JQuery to add a field to a form, I am now stuck on how to implement two add field buttons for adding different fields. Can anyone point me in the right direction? <html> <head> <title>jQuery add / remove textb ...

Determine the overall price of the items in the shopping cart and automatically show it at the bottom without the need for manual entry

Creating a checkout form that displays the total cost of products, subtotal, GST cost, delivery cost, and overall price. The goal is to calculate the total by adding together the costs of all products with the "price" class (may need ids) at a 15% increase ...

Assign a value to the input field based on changes made in another input field

I am brand new to the world of JavaScript and currently grappling with setting a value in an input field based on the onchange event of another input field. Here is my code sample for input field 1: <input type='text' onchange='methodTh ...