Unable to modify the .top attribute style in elements within an arraylist using JavaScript

I need to align multiple images in DIVs on the same line by setting their Top value to match the first image in the array.

Here is the code I am struggling with: (addBorder function is called when divs are clicked)

<div class="DRAGGABLE ui-widget-content ui-draggable" style="border: 1px solid red; width: 120px; height: 60px; top: 10px; left: 10px; position: relative;" onclick="addBorder(event)"> <img id="PMF01" src="/devices/AAU01-010.gif"></img></div>

var selectedDivs = new Array();
function addBorder(e) {
      if ($ctrlBeingpressed == true) {
      e.target.style.border="5px solid green"; // to show they are selected
      selectedDivs.push(e.target);
      }
  }

 function orderTop(){
  for (i=0;i<selectedDivs.length;i++)
    {
    selectedDivs[i].style.top=selectedDivs[0].style.top();
    }
  }

<button type="button" onclick="orderTop()">Align Top</button>

Generated HTML:

<!DOCTYPE html>
<html lang="en">

    <head> … </head>
    <body style="position: absolute; cursor: auto;" application"="" onkeyup="clearPress()> <div id=" onkeydown="controlCheck(event)">
        <div id="accordion" class="ui-accordion ui-widget ui-helper-reset" style="position:absolute;top:0px;left:0px;width:300px;height:900px" role="tablist"> … </div>
        <div id="buttons" style="position:absolute;top:0px;left:315px;width:1100px;height:50px;border:2px solid grey"> … </div>
        <div id="canvas" class="DROPPABLE ui-droppable" style="position:absolute;top:50px;left:315px;width:1100px;height:850px;border:2px solid grey">
            <div class="DRAGGABLE ui-widget-content ui-draggable" style="border: 1px solid red; width: 120px; height: 60px; top: 156.117px; left: 188.117px; position: relative;" onclick="addBorder(event)">
                <img id="PMF00" src="/devices/AAU01-010.gif" style="border: 5px solid green;"></img>
            </div>
            <div class="DRAGGABLE ui-widget-content ui-draggable" style="border: 1px solid red; width: 120px; height: 60px; top: 10px; left: 10px; position: relative;" onclick="addBorder(event)">
                <img id="PMF01" src="/devices/AAU01-010.gif"></img>
            </div>
        </div>
    </body>

</html>

Answer №1

peak does not operate as a function. Please refrain from calling it with ().

Answer №2

Always remember to specify the units (ems, px, %, etc) when setting a top style value. Here's an example:

selectedDivs[i].style.top = selectedDivs[0].style.top + 'px';

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

I'm a beginner when it comes to Android WebView and incorporating JavaScript into my projects

Struggling to make my Android app work with JavaScript, even though I've enabled it. Despite all the research I've done suggesting it should work, it's not. Any assistance would be greatly appreciated. Below is my Java code: protected ...

Retrieving the IDs of all socket connections in a Node.js Socket.IO array

I currently have a complex program utilizing socketio 0.9 where I am storing all the sockets id in arrays like so: var clients = {}; To uniquely identify and store my sockets, I assign them a serial and then set the 'socket' key with its actual ...

Transformed the Next.js Pages Router into an Application Router

Looking to make a change in my API written with Nextjs's Pages Router. Currently, it serves as a proxy for downloads and I am interested in converting it to the App Router method. Does anyone have any guidance on how to go about implementing this? imp ...

What is the best way to ensure that the text input in a text area is linked to a particular radio

I have created a form that contains radio buttons. My next goal is to include a text area next to each group of radio buttons. This will allow the user to select an option either by clicking on the radio button or by typing in the corresponding value in t ...

bespoke design picture holder

Is it possible to create a custom-shaped image container without using <div />? I encounter an issue when trying to add corners on top of the #content-box as shown in this screenshot: . The corner images only cover half of the block element, with th ...

Load the page using AJAX and automatically scroll to the anchor within the loaded content

I want to achieve an automatic scroll to the beginning of a loaded page after using ajax to load it. Here is the code snippet I currently have: html <button class="btn btn-info btn-lg" type="button" id="photos"> <i class="fa fa-plus fa-5x">&l ...

Achieving overlapping of a <div> element with a "position: fixed" container while enabling vertical scrolling of the fixed container's content

In my single page application, I have a fixed container and I am trying to make one div inside this container overlap the boundaries of the fixed container while also vertically scrolling with its contents. Unfortunately, I have only been able to achieve e ...

Issue with React Ref: Invariant Violation when trying to addComponentAsRefTo

I'm encountering an issue while attempting to add a ref to a React component. The error message I'm seeing is as follows: invariant.js:39Uncaught Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding ...

Experiencing issues with autoungrabify or autolock in cytoscape.js?

I have been working on a web application using Cytoscape.js, and I recently integrated the Edgehandles extension to allow users to add edges. The two types of edges that can be added are undirected and directed. Adding directed edges is functioning as expe ...

Adding content to a div element using jQuery is a simple and effective way to

I am attempting to incorporate the content from a specific div into a jQuery function. Kindly refer to the details below: This is the content I want to include: <div id="year">2020</div> My objective is to replace the '2019' within ...

The Material UI button shifts to a different row

I need help adjusting the spacing between text and a button on my webpage. Currently, they are too close to each other with no space in between. How can I add some space without causing the button to move to the next line? const useStyles = makeStyles((the ...

Variables for Angular Material themes

My app has multiple theme files, and I select the theme during runtime. .client1-theme{ // @include angular-material-theme($client1-theme); @include all-component-colors($client1-theme); @include theme-color-grabber($client1-theme, $client1-a ...

How about displaying the Ajax response as an image?

I'm working on a script that pulls an image link, which seems to be functioning correctly. However, I am struggling with how to display the link as an image. Can someone please assist me with this? <script src="//ajax.googleapis.com/ajax/li ...

Convert an HTML table into an Excel file using PHP

After successfully generating DATA from PHP into an HTML table format, I utilized the code below to export it to EXCEL: header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="BASIC_Data.xls"&apos ...

Issue with CSS: Hover effect causing unexpected additional white space

My main goal is to implement a hover effect that covers an entire section, but I'm facing some challenges. When I hover over my products, it doesn't behave as expected and adds extra white space below while not covering the section properly. Thi ...

Automated Form Submission: A Guide to Automatically Sending the Form After Captcha Verification

I'm looking to incorporate ReCaptcha into my website in order to display additional data. My goal is to have the form automatically submitted once the ReCaptcha has been completed, eliminating the need for an extra submit button. However, I've en ...

Custom-designed background featuring unique styles

I have implemented the following code to create a continuous running banner: <style> #myimage { position: fixed; left: 0%; width: 100%; bottom: 0%; background:url("http://static.giga.de/wp-content/uploads/2014/08/tastatur-bild ...

Obtaining the accurate offsetTop and offsetLeft values for an element following a CSS3 rotation

Is there a method to accurately determine the offsetTop and offsetLeft values of an element post-transform rotation? Are there any lesser-known element properties that could be helpful in this scenario? Attached is an image that can provide further clari ...

Parent div is positioned absolutely with overflow set to hidden

I am facing a unique requirement where the parent div has an absolute position and the child div also has an absolute position. Inside the parent div, I have set overflow: hidden to hide the extra width of the child div. However, this setup is not working ...

Angular 11 along with RxJS does not support the combineLatest method in the specified type

Hey there, I'm currently working on utilizing the combineLatest operator to merge two streams in Angular, but I keep encountering an error message stating that "combineLatest does not exist on type". I've attempted to move the code into a .pipe() ...