By incorporating JavaScript, clicking on an image will trigger a shift in the position of a different element

Just starting out with JavaScript and experimenting with creating an event where clicking on an image triggers the movement of another hidden image. The hidden image is positioned off-screen to the left, while there is a table of images in the center of the page. The goal is to have the hidden image slide out from the left when any image in the table is clicked. Currently focusing on learning pure JavaScript without relying on JQuery. Any assistance would be greatly appreciated.

Below is the setup with HTML, CSS, and JavaScript:

HTML for the table:

  <table>
   <tbody>
      <tr>
      <td><img onclick="sleepFunction()" src="01.jpg" height="180" width="120"></td>
      <td><img src="02.jpg" height="180" width="120"/></td>
      <td><img src="03.jpg" height="180" width="290"/></td>
      </tr>
      <tr>
      <td><img src="04.jpg" height="180" width="120"/></td>
      <td><img src="05.jpg" height="180" width="120"/></td>
      <td><img src="06.jpg" height="180" width="290"/></td>
      </tr>
      <tr>
      <td><img src="07.jpg" height="180" width="120"/></td>
      <td><img src="08.jpg" height="180" width="120"/></td>
      <td><img src="09.jpg" height="180" width="290"/></td>
      </tr>
   </tbody>

HTML for the hidden image:

<div class = "sleepImage">
<img src = "sleeping.jpg" width= "490"/>
   </div>

CSS styling:

 .sleepImage {
float: right;
position: absolute;
top: 108px;
left: -600px;
}

Javascript snippet:

function sleepFunction() {
document.getElementsByClassName("sleepImage")[0].style.left = "300px";
}

Answer №1

update the sleepImage by changing it from a class to an id, modify your code like this

html

<div id = "sleepImage">
<img src = "sleeping.jpg" width= "490"/>
</div>

css

#sleepImage {
   /* include your styles here */
}

js

function changeSleepFunction() { 
   document.getElementById("sleepImage").style.left = "300px";
}

Answer №2

` element, create an identifier for your div (NameDiv) and modify your function as shown below:
document.getElementById("NameDiv").style.left = "300px";

Answer №3

When you use document.getElementsByClassName('sleepImage'), it will retrieve an array containing all elements with the class name sleepImage. To target the first element in the array, add an index at the end of the method call. Here's an example:

function adjustSleepImage() {
    document.getElementsByClassName("sleepImage")[0].style.left = "300px";
}

Answer №4

Upon closer observation, the code you have written is as follows:

document.getElementsByClassName("sleepImage").style.left = "300px";

It's important to note the usage of getElementsByClassName. As the name implies, it returns elements, not a single element. These elements are returned as a collection (similar to arrays), so iterating over the collection is necessary to access the specific element you want.

The correct code to use in this case is:

document.getElementsByClassName("sleepImage")[0].style.left = "300px";

Check out the demo below:

document.getElementById("myBtn").addEventListener("click", function () {
    document.getElementsByClassName("sleepImage")[0].style.left = "300px";
});
.sleepImage {
     position: absolute;
 }
<table>
   <tbody>
      <tr>
      <td><img onclick="sleepFunction()" src="01.jpg" height="180" width="120" alt="selectorImage"/></td>
      <td><img src="02.jpg" height="180" width="120"/></td>
      <td><img src="03.jpg" height="180" width="290"/></td>
      </tr>
      <tr>
      <td><img src="04.jpg" height="180" width="120"/></td>
      <td><img src="05.jpg" height="180" width="120"/></td>
      <td><img src="06.jpg" height="180" width="290"/></td>
      </tr>
      <tr>
      <td><img src="07.jpg" height="180" width="120"/></td>
      <td><img src="08.jpg" height="180" width="120"/></td>
      <td><img src="09.jpg" height="180" width="290"/></td>
      </tr>
   </tbody>
       </table>
<input type="button" value="Move Image" id="myBtn"/>
       <div class = "sleepImage">
<img src = "sleeping.jpg" width= "490" alt="sleepImage"/>
   </div>

I hope this clarifies things for you!

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

Ways to guide users through a single-page website using the URL bar

I currently have a one-page website with links like <a href="#block1">link1</a>. When clicked, the browser address bar displays site.com/#block1 I am looking to modify this so that the browser shows site.com/block1, and when the link is clicke ...

Conditionally displaying ng-options in AngularJSI'm looking for a

After spending hours searching, I'm unable to find a solution to my problem. I was able to implement it before but lost the code and can't remember how I did it. I need to display only certain array values in a select box using ng-options. The d ...

Issue with fixed positioning not behaving as intended while scrolling downwards

I am facing an issue with an element that has the .it class and I want to keep it fixed on the screen using the position: fixed CSS property. However, as I scroll down the page, the element moves along with the screen instead of staying in place. Below is ...

Warning: Unhandled promise rejection - The type error occurred because the property 'close' of the object is undefined

I am currently configuring node.js in order to test my JavaScript codes using the Jest JavaScript testing framework. Can anyone spot what I might have done incorrectly? package.json file { "name": "institute-jest", "version&quo ...

Access to property 'nodeType' on AJAX request in Firefox has been denied due to permission error

On my webpage, I have integrated the Google Sign-in button along with gapi for interaction. After a successful authentication using the Google API, an AJAX call is made to the server using JQuery: var token = gapi.auth.getToken(); var data = { "to ...

Extract form input to utilize in nodemailer

Currently I am working on a form implementation where I intend to utilize nodemailer for sending test emails to myself. My goal is to have the email inputted into the form dynamically appear in both the "to" and "subject" fields when the user submits the f ...

Instructions for adding and deleting input text boxes on an ASP.NET master page

*I am currently facing an issue with adding and removing input textboxes for a CV form using ASP.NET in a master page. Whenever I click on the icon, it doesn't seem to work as intended. The idea is to have a textbox and an icon "+" to add more textbox ...

Joi validation that focuses on required array elements while disregarding nested keys

Why is my Joi array required validation not detecting an empty array? I have an array called userData that contains objects with keys dateMilli and value. Despite adding required validations everywhere, passing an empty array [] for userData does not resul ...

How can I restrict website access to only specific IP addresses?

I am looking to restrict access to certain IP addresses on a website, but the issue is that dynamic IPs keep changing based on the source of internet connection. Is there a method to determine the static IP of the computer rather than relying on the dyna ...

How can I use JQuery to sum up the numbers within div elements?

Is there a way to automatically number the generated blocks? For example, the first block would display "1", the second "2" and so on. Below is my current code. Simply input the desired number of blocks in the input field. <!DOCTYPE html> <html ...

Enhancing Website Interactivity with PHP, AJAX, and

I recently followed a tutorial on handling AJAX requests with PHP and MySQL, which can be found here. My goal is to update the SQL query based on the value selected from a dropdown menu using the onchange event. function myfunctionTime(time) { if (t ...

Updating elements within a subarray in JavaScript can easily be accomplished by accessing the

I need help updating nested array elements in JavaScript. I want to convert dates into a different format. How can I update the nested elements? array1 = [ { "week": [ "2019-05-06T16:00:00.000Z", "2019-05-07T16:00:00.000Z", "2019-05-08T16:00:00.000Z", "20 ...

Setting up a Form submit button in Angular/TypeScript that waits for a service call to finish before submission

I have been working on setting up a form submission process where a field within the form is connected to its own service in the application. My goal is to have the submit button trigger the service call for that specific field, wait for it to complete suc ...

Tips for disabling autofocus on Mui Menu Items

Incorporating a search functionality within a custom Mui Select component where the user can input a city or country to filter down the options list. The current issue is that when a user types a letter that matches the first letter of an option, the opt ...

Struggling to Align Banner in Dynamic Layout

I am currently working on centering the banner image as the resolution size decreases, rather than having it pushed to the right. The specific page I am addressing can be found here. My approach involves utilizing a width of 100%. <!-- BEGIN T ...

Is it possible to terminate an active server process triggered by an HTTP post request in Node.js prior to returning a response?

I developed an application where I utilized Ajax to make calls to a Node server. The issue is that even if the user navigates to another page, the server persists in processing the initial request made by the Ajax call. It then proceeds to process the new ...

Remove buttons from carousel elements in React Multi-Carousel

Is there a way to hide the arrows in the react-multi-carousel component? https://i.stack.imgur.com/V1nix.png ...

Modify the bootstrap form dynamically in response to the user's input. Update the form layout instantly as the user types, with no need for clicking any buttons

Imagine a scenario where, as soon as you enter your credit card number, the form automatically undergoes a change without requiring a button click, similar to how a credit card logo pops up. The form detects changes instantly after the input field has be ...

Determining the size of an object in ASP.net as a percentage of the page, while taking into account the size of

I need help adjusting the size of a silverlight app on a page with multiple banners above it. My goal is to make the app's height always 100% of the screen height minus the banner's height to prevent any scroll bars from appearing. Does anyone kn ...

How to Populate Radio Buttons in HTML with Data from a MySQL Database

I am seeking assistance with a PHP and MySQL project since I am new to these technologies. Any help would be greatly appreciated! I have created a second table that contains inventory information. This table includes two columns: Manufacturer and Item. O ...