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

Sending data from an AngularJS app to Google App Engine using HTTP post

I'm facing a small issue where I am unable to successfully POST my data (comments) to the GAE datastore using angularjs. Can you please help me identify what's wrong with the following angularjs "post" or html code? ANGULAR: $scope.addComment = ...

The aggregation pipeline in nodeJS with mongoDB is failing to return any results, returning an empty array instead

Currently, I am enrolled in Jonas Schmeddtman's Node.js course where I am working on developing a tour App. However, I have encountered an issue where sending a request via Postman on the specified route results in an empty array instead of the expect ...

"Troubleshooting the issue of null values in ASP.NET Core Razor Pages when using Ajax POST

I'm currently working on an ASP.NET (6.0) Razor Pages project where I am facing an issue with posting data using AJAX. Despite my efforts, the posted data always ends up being null. Even after going through similar questions on Stack Overflow, I have ...

What is preventing me from merging these two arrays together?

Here is some code for a Vuex mutation: export const CREATE_PANORAMAS = (state, panoramas) => { console.log('building.panoramas:', state.building.panoramas) console.log('panoramas:', panoramas) state.building.panoramas.concat(p ...

Elements of the user interface used for inputting an activation code

Seeking input on a usability issue I'm facing on my site. The homepage of my site has two sets of controls - one for user login and the other for new users to activate their accounts. The problem lies in the activation process. Users receive an acti ...

Is your Angular 2 routing failing to work properly after a page refresh or reload when using gulp?

I've recently started learning Angular 2, and I encountered an issue with routing. During the development phase, I ran my application using npm start. However, after migrating to gulp.js, when I run the application by typing gulp, everything works fin ...

What is preventing me from translating JS Canvas Image?

I've been struggling with using ctx.translate() to translate images on a canvas. No matter what I do, it just won't work. I've spent a good amount of time trying to troubleshoot the issue. Here's the snippet of code I'm working wit ...

How can I modify the mesh structure in Three.js?

Having two meshes, mesh1 and mesh2, each with the same number of vertices and extrusion. mesh1 = 5000 vertices. mesh2 = 5000 vertices. I transfer the vertices from mesh2 to mesh1. Then I execute: mesh2.geometry.verticesNeedUpdate = true; mesh2.geometry. ...

React - dynamically injecting external logic during execution

My goal is to modularize my React application by loading additional logic (such as containers/components) dynamically from an external .js file during runtime. For instance, I want to be able to introduce a new tab with completely different functionality o ...

How can you disable an 'option' HTML5 element using the AngularJS methodology?

How can I disable an 'option' element in HTML5 using AngularJS? Currently, I am working with AngularJS v1.2.25. Here is the Plunk Link for reference: https://plnkr.co/edit/fS1uKZ <!-- CODE BEGIN --> <!DOCTYPE html> <html> ...

Exploring the realms of Django Administrator with external JavaScript integration

Currently, I am working with django 2.0 that includes jquery 2.2.3. My goal is to implement the ImageViewer Javascript app (https://github.com/s-yadav/ImageViewer) on one of my admin pages. I have added the necessary js and css files to the Media class wit ...

Interacting with Rails controllers through AJAX to trigger a JavaScript function

After exploring numerous stack overflow posts without finding a working solution, I decided to seek help for a well-documented use case. I have a button that should perform the following tasks: When clicked, call a custom controller method to update th ...

How to access JavaScript files from "bower_components" instead of "node_modules" using webpack

With the utilization of main-bower-files in my Gulp compilation tasks, it is not feasible for me to use webpack to require modules from the node_modules directory as it would interfere with the processing of CSS, images, and fonts in my current asset sys ...

The integration of jQuery in PHP is experiencing some technical challenges

There's one issue driving me crazy: I created a left-floating file tree view and included it in my website. However, when I include my website in the file treeview, it becomes unclickable in Chrome, and in IE 9, the thumbnail part for viewing PDF docu ...

Is it possible to create Android apps using HTML and JavaScript?

I have a strong foundation in HTML, Javascript, CSS, and AJAX, but I want to venture into Android application development. However, I lack knowledge of Java technology. Is it feasible to develop Android apps using HTML or JS? If anyone has experience wit ...

How to send variables to a function when a value changes in TypeScript and Angular

As someone who is new to Angular and HTML, I am seeking assistance with the following code snippet: <mat-form-field> <mat-select (valueChange)="changeStatus(list.name, card.name)"> <mat-option *ngFor="let i of lists"> {{i.name}} ...

Tips for removing unnecessary debugging code from JavaScript when compiling or minifying your code

Back in the day, I would always include tons of debug code in my JavaScript app. However, I'm now searching for a method that will eliminate debug code during the compilation/minification phase. Does the world of JavaScript have something similar to ...

Comprehend PHP scripts within a separate JavaScript file

$(".btn-delete-news").click(function() { if (!confirm('Are you sure to delete ?')) return false; var idNews = $(this).attr('news-info'); console.log(idNews); $.ajax({ url:"<?php echo Yii::app()->createUrl( ...

Define the dimensions of the container div that houses the SVG text element

<div id="maindiv" style="height:60px;width:100px;border:1px solid black;float:left"> <svg width="100%" height="20"> <text x="28" y="18" style="text-anchor: start">somedynamictext</text> </svg> </div> Is ther ...

JQuery is having trouble locating a variable in a different JavaScript file

Currently, I am utilizing the cakephp framework and have developed 2 distinct javascript files which I have stored in my webroot/js directory. The first javascript file includes modal dialog variables that define the settings for the dialog boxes. The seco ...