Utilizing Javascript Targeting within Conditional Statements

Currently, I have two images in my web project...

function transformImage(el) {
  if (el.className == "front") {
    el.className = "back";
    getElementById("right").className = "right1";
  } else {
    el.className = "front";
    getElementById("right1").className = "right";
  }
}
.front {
  position: absolute;
  bottom: 370px;
  opacity: .5;
}
.right {
  position: absolute;
  bottom: 370px;
  left: 1637px;
  opacity: .5;
}
.back {
  position: absolute;
  bottom: 370px;
  left: 500px;
  opacity: 1;
}
.right1 {
  position: absolute;
  bottom: 370px;
  left: 1137px;
  opacity: 1;
}
<img src="img.png" width="290" height="228" id="front" class="front" onclick="transformImage(this)">
<img src="img.png" width="290" height="228" id="right" class="right" onclick="moveRight(this)">

I am currently working on modifying these images by changing their classes when clicked. Here's a sample of the JavaScript code:

The ultimate goal is to alter both images with new CSS properties as displayed below:

When implemented correctly, both images are expected to move and adjust their opacities. Unfortunately, only the image tagged with the "front"/"back" class seems to be responding while the "right"/"right1" image remains unchanged. It appears that there might be an issue with my JavaScript, but I'm not sure where the problem lies. Any guidance on this matter would be highly appreciated!

This is my initial attempt at writing code, so I appreciate your patience if I'm missing something obvious.

Thank you for your time and assistance! :)

Answer №1

To select an element by its ID in JavaScript, you should provide a string as an argument to the getElementById method and invoke it on the document object:

document.getElementById("ra").className = "ra1";
...
document.getElementById("ra1").className = "ra";

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

Determining when all promises have either been rejected or resolved using vanilla JavaScript promises

In an effort to transition to loading all resources in my web application asynchronously using basic JS promises, I have implemented a ScriptLoader function. Below is the code snippet for the ScriptLoader used to load .js files: function ScriptLoader() { ...

Unable to execute redirect function in Next.js 14 application

I've been working on implementing basic authentication within a Next.js app, however I'm encountering issues with redirecting to the homepage from the auth.ts file. Below is a snippet of the code where I am implementing the loginForm: //loginForm ...

Highcharts memory leakage issue arises when working with jQuery version 2.X

After extensive testing on my AngularJS application, I have discovered a memory leak when using Highcharts with the latest version of jQuery (2.1.4). Below are links to two plunkers for comparison: Using jQuery 1.8.2: http://plnkr.co/edit/lQ6n5Eo2wHqt35OV ...

JSON.stringify not behaving as anticipated

I am working on the code below; var data = []; data['id'] = 105; data['authenticated'] = true; console.log(data); var jsonData = JSON.stringify(data); console.log(jsonData); The initial console.log is displaying; [id: 105, authenti ...

Router-view failing to display component

I am currently working on setting up basic routing in Vue. I have identified three file listings that seem to be causing issues. When I include <router-view> in app.vue, the foo component appears in the browser. However, when I follow the instruction ...

Validating dropdown lists with Jquery

Custom Dropdownlist: <div class="col-md-2"> <div class="form-group"> <label for="field-3" class="control-label">Priority</label> <select id="lstpriority" class="custom-selectpicker" data-live-search="true" da ...

Creating a dynamic dropdown menu that displays options based on the selection made in a previous drop

Attempting to replicate this exact functionality on my own site has proven difficult. Despite success on jsfiddle, none of the browsers I've tested seem to cooperate. Any suggestions? Even when isolated as the sole element on a page, it simply refuses ...

Utilize the native HTML attribute to capture the mouse wheel event

I'm interested in utilizing the mousewheel event in my project, but all the information I've found online relies on addEventListener(). I want to detect it using native HTML and CSS. In simpler terms, I'm hoping for something along the lines ...

Retrieve the value of a style property using the css() method

<p class="pen" style="color:#000">abc</p> .pen{ color:#333 } In the previous code snippet, we have an HTML element with inline style and a CSS class that defines color properties. Despite the inline style having higher precedence, jQuery retu ...

Tips for creating an input box that only accepts floating point numbers:

I have a custom component - a text box that I am using in two different places. In one location, it accepts integers and in another, floats. For the integer validation (where dataType=2), I have successfully implemented it. However, for the float validat ...

I am experiencing an issue where the Axios configuration does not display the OnUploadProgress on my response

I have been attempting to track the progress of file uploads from the front end, but I am encountering an issue where the onUploadProgress is not being received in the configuration catch. This problem arises as I am relatively new to using Axios. axios({ ...

Transferring information from one webpage to another using AJAX or embedding it with an iframe

I recently received an address with a basic HTML structure containing numbers. I attempted to display it using an iframe, which worked when tested separately but encountered a connection refusal error when embedded in my page. Alternatively, I tried AJAX ...

What could be causing the malfunction of this setTimeout function? Another related question to consider

I recently implemented a script on one of my pages, but I noticed that the setTimeout function doesn't seem to be triggering. Currently, it's just set up to display an alert as a test. I suspect that the issue might be related to a meta refresh t ...

The component data fails to reflect the updated value following a status change due to not properly retrieving the new result from the POST function

Below is the Vue.js 2 code snippet for sending data to the backend. The vuex library was used to manage the data status. After using the POST function, the result returned from the backend updates the value of sampleId. This value is auto-generated by the ...

Dexie: scheduling happenings within a specified date range

Currently, I am utilizing the Dexie library for IndexedDb. As I delve into the API documentation, my focus is on finding a way to select events that fall within two specific dates. It appears that this can be achieved using the .between() method in the fol ...

The container cannot contain the overflowing DIV

I have 4 different divisions named outer, inner, title, and content. My goal is to place the inner div inside the outer div, with both the title and content divs positioned within the inner div, one above the other. I have set the outer and inner divs to b ...

Issue with selecting file not triggering input file selection event in KnockoutJS and HTML when the same file is selected

Struggling to find a solution using KnockoutJS instead of jQuery. When the same file is selected, the event doesn't fire. Here is an example of the HTML: <label class="upload"> <input id="documen ...

Unexpectedly receiving an empty string from $_FILES

Here is the code for my form: html += `</div> <div class="card-footer"> <form id="messageSendDashboardGroup" enctype="multipart/form-data"> <div class="image-upload"> <div class="input-group" > ...

What could be the reason behind the validation failure of this Javascript code?

After implementing your recommendation, this is my current status: <script> function tick() { const React.element = ( '<div><marquee behavior="scroll" bgcolor="lightyellow" loop="-1" width="100%"> <i> <font color ...

The detailView feature in wenzhixin bootstrap-table is failing to initialize the child row

I am currently utilizing the wenzhixin bootstrap table and attempting to initialize another bootstrap table within a child row known as 'detailView' in the code. Previously, I was using datatables but found this method to be simpler and more dir ...