What are the best methods for preserving paint color when changing wheel styles, and vice versa?

I posted this query about a year ago, but I did not have a fiddle prepared at that time and my question was not as well articulated as it could have been. Despite that, many people came forward to offer their help, and I am truly grateful for that. This time around, I have created a fiddle with the hope of providing you all with a better understanding of what I need assistance with. Here is the link to the fiddle

If necessary, here is the full page - Visit the full page here - all the code can be found on the page itself.

My aim is to create a mock car selector page to enhance my knowledge of JavaScript. I want users to be able to choose a car color first, followed by a wheel style. The selected car color should remain constant while cycling through different wheel styles, and vice versa. Users should also be given the option to select a wheel style and then cycle through the available colors while keeping the chosen wheel design intact, or opt for a paint color and navigate through the various wheels.

Currently, there are options to select from five different wheel types or sixteen distinct colors, but these choices are independent of each other. I am struggling to come up with the JavaScript logic required to tie them together.

I have images for all 16 unique paint colors with each of the 5 wheel styles, totaling to 80 pictures in total.

Some of the approaches I've considered/tried include...

  • Creating an array containing all 80 image paths and calling the appropriate one when needed
  • Adding 80 image tags in the HTML to introduce them into the DOM, setting the image class to display: none; and later changing it to display: block; onClick

Due to my lack of experience, I am struggling to develop the required logic and feel stuck at the moment. Below is a glimpse of the current JavaScript logic I am utilizing.

... (JavaScript functions go here)

Answer №1

In order to streamline your code, consider iterating over your selectable DOM elements...

Next, make the necessary call on the site to showcase the car and wheels...

<img src="https://www.uniquecoder.com/porsche-working/images/carerra-s/color15-option3.png" alt="911 Carerra S" id="carerra" />

You have a color choice and an option for the wheels. => color15-option3

Simply input the correct index of the color and wheel here to reconstruct your SRC link for displaying the appropriate image reflecting your choices.

The color index can be determined by utilizing dataset attributes in your HTML for both the input and wheel elements.

For example, if the following HTML line represents the fifth available color, it will have a data-id of 5:

<input id="exterior-swatch5" data-id="5" type="button" value=" " class="swatch" />

Add a dataset attribute for each selectable element corresponding to the correct string required to plug into your SRC attribute for fetching the desired image from your site.

A single function can handle two event listeners - one for the color and another for the wheels. Both events trigger the same function to inspect the class of the event target.

Then update the src attribute to display the desired image.

document.getElementById('carerra').src = "https://www.uniquecoder.com/porsche-working/images/carerra-s/color" + selectedColor + "-option" + wheelOption + ".png";

Note: Retrieve the default color and wheel settings from your HTML via a couple of .split functions applied to the default src string. By using this method, define the default wheel and color values, then input them whenever the click event is activated using a conditional statement.

Here is the customized CSS and HTML used in this functionality...

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

Concealing specific DIV elements (unfortunately not nested)

Currently, I am dealing with pre-existing code that is automatically generated and needs to adhere to a specific format: <div id="TITLE1"></div> <div id="div-1"></div> <div id="div-2"></div> <div id="div-3"></d ...

Identifying Flash content in a unique way

In my dynamic page (let's call it myFlashContainer.jsp), the Flash content changes based on the link that is clicked. The code responsible for rendering the Flash looks like this: <object height="100%" align="l" width="100%" id="player" codebase= ...

Adding a div to a different webpage using jQuery

I am currently working on a small admin panel and I was curious if it is feasible to use jQuery to add a div box inside the index.php file after clicking a button in the panel? If this is possible, an example demonstrating how to do so would be greatly ap ...

What is the best way to resize a Drawable to fit the width of a TextView?

When trying to render HTML with image tags within a TextView, I encountered issues with scaling the images using the getDrawable() code. Here is an example snippet of the code: myTextView.setText(Html.fromHtml(htmlSource, this, this)); @Override public Dr ...

The jQuery CSS menu is having trouble highlighting the active link

Can you help me figure out what's wrong with this code and how I can fix it? I'm working on a CSS menu (horizontal) where I want the background and font to change when an item is selected. I found some jQuery code from another post that should m ...

JavaScript fails to perform without the presence of an alert

Hey there, I've come across a bit of a puzzling situation and I'm struggling to find the right words for the Title of this question. I have a javascript function that is supposed to trigger when the document has finished loading. In the initial p ...

centered table within a container with vertical alignment

I'm having trouble centering a table vertically in a div. I've tried using vertical-align but it's not working for me. Can anyone suggest a solution to place it at the top of the div? <div class="col-12 d-flex"> <div c ...

Is there a way to create a flexbox that combines a centered fixed child with a fixed child at the bottom?

I'm trying to create a layout using flexbox that looks like this: | | | | | CENTER FIXED ELEMENT | | | | BOTTOM FIXED ELEMENT | Here is the flexbox CSS I've attempted: .wrapper{ ...

Fetching the added information using ajax

Hey everyone, I have a question about reloading the appended data on my page. The page has infinite scrolling functionality and I want to reload the elements that are added when a particular element is clicked. The loading works fine for elements that were ...

What could be causing my for loop to unexpectedly terminate early?

I'm currently tackling a challenge on CodeChefs: My task is to find the smallest missing positive integer in an unsorted list of numbers. Here's the code snippet I've implemented: var firstMissingPositive = function(nums) { nums.sort(); ...

Steps for compiling SCSS to CSS using Angular-CLI and moving it to the assets directory

I am currently working on an Angular 5 project with an assets folder where I store my common CSS file and reference it in index.html. I now plan to create a new folder called "sasstyles" and place some .scss files there. When I compile or run the project ...

Encountered a TypeError in Angular printjs: Object(...) function not recognized

I'm currently working on integrating the printJS library into an Angular project to print an image in PNG format. To begin, I added the following import statement: import { printJS } from "print-js/dist/print.min.js"; Next, I implemented the pri ...

Enhancing jQuery UI Tabs with HoverIntent

I'm currently working on incorporating HoverIntent into my jQuery tabs, but I seem to be facing some issues. Below is the code snippet that I've been using. Any suggestions or tips would be greatly appreciated. Thank you! The code was sourced di ...

Tips for Customizing the Width of Your Material UI Alert Bar

At the moment, I have refrained from using any additional styling or .css files on my webpage. The width of the Alert element currently spans across the entire page. Despite my attempts to specify the width in the code snippet below, no noticeable change ...

What is the reason behind Google Closure Compiler appending a variable to the global namespace when the original namespace was blank?

My long script is neatly enclosed within a (function() {/.../})() to prevent any name pollution. It has been typed with complete accuracy and zero warnings. I recently discovered that Google Closure compiler initially redefines i and j in the global names ...

PHP dynamically creates thumbnails that hyperlink to additional images

Below is my PHP code: $thumbs = glob('images/*_thumb.jpg'); $thumbs_number = count($thumbs); $watermarked = glob('images/*_watermarked.jpg'); $watermarked_number = count($znaki); for ($i = 0; $i < $thumbs_number; $i++) { echo "< ...

Learn the process of uploading a default file with C# in ASP.NET

Is there a way to upload a default file from the client side without having to use the browse button in the file upload control? For example, I want to upload a specific file every time I click a button, without showing the file dialog. Can this be achie ...

Eliminate unneeded null and empty object data attributes within Vue.js

When sending object data to an API, I have multiple states but not all of them are required every time. Therefore, I would like to remove any properties from the object that are null or empty strings. How can I achieve this? export default { methods: { ...

Attempting to adjust numerical values to display with precise two decimal places using jQuery

Possible Redundancy: JavaScript: how to format a number with only two decimal places I'm getting confused while using variables and now I can't seem to get the calculation working correctly. Any ideas? $("#discount").change(function(){ ...

A function cannot be used with Random Song Loop

Recently delving into the world of JavaScript, I encountered a peculiar issue. When attempting to execute the following code within a function, nothing displays in the console. Yet, after testing it without the function, the expected strings appear as inte ...