Retrieving the `top` value using `$this.css("top") can either return an object or an element value

Something odd is happening with my HTML object:

<div data-x="1" data-y="1" class="tile empty" style="top: 32px; left: 434px;"> 
   <div class="inner">1:1</div>
</div>

When attempting to access its top property in jQuery using the code below:

$tile = $('[data-x=1][data-y=1]'); 
top = parseInt( $tile.css("top") );

And then displaying it with:

console.log(top);

The browser outputs this instead:

Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}

I'm puzzled by this behavior, even after removing part of the code from - $img.height() to top as it's needed for another element later on.

My expectation was to see 32px returned, which would be parsed and output as 32;

Answer №1

top is a global variable that cannot be reassigned in browsers. The value you are seeing is the standard top-level window.

Here are some recommendations:

  1. Always give your code a local scope instead of leaving it at the global scope.

  2. Declare your variables within that local scope.

  3. Utilize strict mode to catch errors like trying to assign values to read-only variables. Strict mode also helps in preventing implicit globals, as discussed in this blog post.

For example:

(function() {
    "use strict";
    // ...
    var $tile = $('[data-x=1][data-y=1]'); 
    var top = parseInt( $tile.css("top") );
    // ...
})();

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

Vue. A variable that changes dynamically in v-if conditions

How can I include a variable in a v-if statement based on my specific situation? I have a data variable called language = 'en' and a large json object with data in multiple languages (referred to as message). Here is an example of the structure o ...

Utilizing JavaScript Modules to Improve Decoupling of DOM Elements

Currently, I am tackling portions of a complex JavaScript application that heavily involves DOM elements. My goal is to begin modularizing the code and decoupling it. While I have come across some helpful examples, one particular issue perplexes me: should ...

Loading Web Applications Screen

My web app contains multiple tree views. Upon loading the page, I first see the unordered lists before the styling of the trees is displayed in the DOM after a brief delay. Is there a method to mask the web app with a spinner placed in the center of the s ...

Tips for implementing a jQuery script within an Angular directive

I recently attempted to incorporate a jQuery code into an Angular component, but I encountered some issues. The jQuery code was originally placed in the HTML, which is considered bad practice. So, I tried creating a new function and adding my jQuery code t ...

Transferring a FormData object to the server using Ajax (including file uploads)

I'm facing an issue with my form that includes both text and file fields. My goal is to submit the entire form to a php script hosted on the server, and then receive a validation message in return. Here's how my form is structured: <form ...

Troubleshooting common issues with PHP's simple HTML DOM parser

I've encountered an issue while working on a scraper for a website that involves crawling through links. The error message I'm receiving is as follows: PHP Fatal error: Uncaught Error: Call to a member function find() on null in D:\Projekti ...

Align Image According to Dimensions of the Browser

I'm looking for a way to dynamically position an image based on the width and height of the browser within an HTML file. I know that adjusting an image's width/height is possible through code like this: <script> ...

The error message indicates that the argument cannot be assigned to the parameter type 'AxiosRequestConfig'

I am working on a React app using Typescript, where I fetch a list of items from MongoDB. I want to implement the functionality to delete items from this list. The list is displayed in the app and each item has a corresponding delete button. However, when ...

Tips for sequentially arranging and rearranging an array of numbers, even when duplicates are present

Encountered a perplexing issue that has me scratching my head in an attempt to visualize a solution. Currently, I am working with an array of objects that appears as follows: let approvers = [{order:1, dueDate: someDate},{order:2, dueDate: someDate}, ...

What is the process for loading a script file from within a component's HTML?

My goal was to include a specific script file in the component html, but I noticed that when I added the script reference within the html itself, the inner script file was not rendered along with the component on the page. Component import { Component } ...

Refresh tab URLs for dynamic tabs with jQuery UI

Utilizing jQuery UI dynamic tabs in my application requires updating the URL hash value when a user clicks on a tab. After researching solutions on SO, like link1 and link2, I attempted the following approach: Javascript: $( "#tabs" ).tabs({ select ...

AngularJS - Import and save CSV files

I have set up a nodeJS program as the server and an AngularJS web application as the client. For generating CSV files, I am utilizing the "express-csv" library (https://www.npmjs.com/package/express-csv) Below is the code for the server side: Definition ...

Interactive pop-up window featuring conversational chat boxes

Trying to create a comment box within a Modal dialog box that is half of the width. The comments in this box should be read-only and created using div tags. Attempted reducing the width and using col-xs-6, but ending up with columns spanning the entire w ...

Despite implementing desandro and new jQuery, the tumblr masonry + infinite scrolling feature still causes posts to overlap on the

Hi there, I am relatively new to the world of javascript and currently facing a frustrating issue with masonry and infinite scroll on my Tumblr blog. I have scoured through various forum discussions regarding these problems but unfortunately, none of the s ...

Exploring High-Density Mobile Devices with Bootstrap 3 Landscape Mode

After completing my first Bootstrap site using version 3, I noticed a display issue when viewing it on an iPhone 5 or LG G2 in landscape mode due to the pixel density. This was not a problem with the Responsive Grid system I previously used. Although I sp ...

Position DIVs next to each other

After scouring through countless Stack Overflow threads in search of a solution to my issue, I still can't seem to get it right. Everyone suggests using float: left, float:right, overflow: hidden, display: block, etc., but none of them are working for ...

Troubleshooting issue: jQuery/Ajax selector not functioning as expected

After using jQuery.ajax() to fetch HTML content from a PHP source and inject it into an element on the page, I've noticed that jQuery selectors do not work on the elements within that loaded HTML. Is this a common issue or should I provide an example ...

Using jQuery to smoothly scroll to a position determined by a custom variable

Here's the script I am working with: $('.how-we-do-it .items div').on('click', function () { var $matchingDIV = $('.how-we-do-it .sections .section .content div.' + $(this).attr('class')); $matchingDIV. ...

Include a new button in the react material-table toolbar

I am looking to enhance the material-table toolbar by adding a new button. This button will not be directly related to the table, but instead, it will open a modal window with additional information. The button I want to add is called "quotations" and I w ...

What is the best way to access a specific element within a component from a different component?

Seeking assistance with communication issues between React components. I have a Container component containing child components Contact, More, and About for a single-page website. Each child component has a reference set. The problem arises when trying to ...