Show picture once an option has been selected from the drop-down menu

I'm looking to find out how I can change images using color div changers.

For example, if I have a dropdown menu with different car brands (like Ford, GMC, Nissan), and I select a vehicle which is then categorized by model. If I choose a model, a default image will display from that model.

Now, I want to be able to change the color of the model vehicle using div color boxes. For instance, if I select blue, I want an image to display with the make, model, and color without showing all the color divs with the models.

If you need more clarification on what I'm trying to achieve, feel free to ask questions!

 function fctCheck(brand) {
   var elems = document.getElementsByName("subselector");
   for (var i = 0; i < elems.length; i++) {
     elems.item(i).style.display = "none";
   }
   document.getElementById(brand).style.display = "block";
 }

 function setCar() {
   var img = document.getElementById("image");
   img.src = this.value;
   return false;
 }
 document.getElementById("ford").onchange = setCar;

 function setCar() {
   var img = document.getElementById("image");
   img.src = this.value;
   return false;
 }
 document.getElementById("gmc").onchange = setCar;

 function setCar() {
   var img = document.getElementById("image");
   img.src = this.value;
   return false;
 }
 document.getElementById("nissan").onchange = setCar;

 function setCar() {
   var img = document.getElementById("image");
   img.src = this.value;
   return false;
 }
 document.getElementById("dodge").onchange = setCar;
.foo {
  float: left;
  width: 20px;
  height: 20px;
  margin: 5px;
  border: 1px solid rgba(0, 0, 0, .2);
}
.white {
  background: #FFFFFF;
}
.yellow {
  background: #FAFF38;
}
orange {
  background: #FFA200;
}

.red {
  background: #FF0000;
}
dorange {
  background: #FF5500;
}
lgreen {
  background: #80FF00;
}

green {
  background: #45C731;
}

turk {
  background: #17DDBC;
}
lblue {
  background: #00A2FF;
}.blue {
  background: #1713F6;
}.purple {
  background: #AB09D3;
}.black {
  background: #000000;
}
​
<div id="colour">
            <div class="foo white" data-color="#FFFFFF">
            </div>
            <div class="foo black" data-color="#000000">
            </div>
            <div class="foo yellow" data-color="#FAFF38">
            </div>
            <div class="foo orange" data-color="#FFA200">
            </div>
            <div class="foo red" data-color="#FF0000">
            </div>
            <div class="foo dorange" data-color="#FF5500">
            </div>
            <div class="foo lgreen" data-color="#80FF00">
            </div>
            <div class="foo green" data-color="#45C731">
            </div>
            <div class="foo turk" data-color="#17DDBC">
            </div>
            <div class="foo lblue" data-color="#00A2ff">
            </div>
            <div class="foo blue" data-color="#1713F6">
            </div>
            <div class="foo purple" data-color="#AB09D3">
            </div>
        </div>

<select id="brand" onchange="fctCheck(this.value);">
  <option value="">Choose a brand</option>
  <option value="ford">Ford</option>
  <option value="gmc">GMC</option>
  <option value="nissan">Nissan</option>
  <option value="dodge">Dodge</option>
</select>


<img id="image" src="Null_Image.png" />
<select id="ford" name="subselector" style="display:none">
  <option value="http://mebe.co/mustang">Mustang</option>
  <option value="http://mebe.co/f150">F150</option>
</select>

<select id="gmc" name="subselector" style="display:none">
  <option value="http://mebe.co/yukon">Yukon</option>
  <option value="http://mebe.co/1500">1500</option>
</select>

<select id="nissan" name="subselector" style="display:none">
  <option value="http://mebe.co/sentra">Sentra</option>
  <option value="http://mebe.co/gtr">GTR35</option>
</select>

<select id="dodge" name="subselector" style="display:none">
  <option value="http://mebe.co/dart">Dart</option>
  <option value="http://mebe.co/challenger">Challenger</option>
</select>

Answer №1

   let chosenCar;//Declare the variable for the car in global scope

   function selectCar(selectedCar) {
       let image = document.getElementById("image");
       image.src = selectedCar + "_default.jpg";//eg. dodge_default.jpg
       chosenCar = selectedCar;//set the value of `theCar` to the currently selected car
       }

   document.getElementById("brand").onchange = selectCar(this.value);
   //etc.

     function pickColor(selectedColor){
     let img = document.getElementById("image");
     img.src = chosenCar + "_" + selectedColor + ".jpg";//eg. dodge_blue.jpg
     }

     let colorBoxes=document.getElementsByClassName("foo");//select all the color boxes
     for(let i = 0; i < colorBoxes.length; i++) {//loop through them
     colorBoxes[i].addEventListener('click', pickColor(colorBoxes[i].dataset.colorName));//and attach to each the event lisner for a click which fires the function pickColor()
     }

Note, an extra data attribute data-colorName has been added to your color boxes that must match the suffix of your image names eg.data-colorName="blue"

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

Two headings using the same tags appear distinctively rendered

Recently, we added an iframe to our company's website that I have been helping manage. This iframe is responsible for handling requests sent to our organization. Interestingly, the headings on our website are styled using the following CSS: color: # ...

Trigger a JavaScript function upon clicking a link

Is it possible to have a CMS that loads articles using ajax, where the article is loaded through a function with parameters, and when a certain link is clicked, it redirects to the target page and launches the function on that page? For instance, let' ...

How can I populate a textbox with the price of a selected item when I choose its item code from a combobox?

I have been working on a code to display a value in a textbox based on the selected value from a combobox. The current functionality is working fine - when I select an item from the dropdown, the name of the item is displayed in the textbox. However, what ...

Exploring VueJs 3's Composition API with Jest: Testing the emission of input component events

I need help testing the event emitting functionality of a VueJs 3 input component. Below is my current code: TextInput <template> <input v-model="input" /> </template> <script> import { watch } from '@vue/composition-api&ap ...

Memory problem with rendering text on a 2D canvas in ThreeJs

How can I optimize my code for rendering nearly 200 texts using 2D canvas rendering? The current method is consuming a lot of memory. Is there a way to reuse the canvas element? Here is a snippet of my current implementation: var goalCanvas = document.c ...

Initiate a Material-UI CSS animation when the element comes into the viewport

After conducting some research on CSS animation triggers when an element comes into view, I came across this solution that utilizes the IntersectionObserver and element.classList.add('.some-class-name') While this method is demonstrated in pure ...

Angular JS allows for the creation of a multiple select feature with an additional value linked to each selection

Looking for advice on how to add an additional integer value along with selected values in a multiple select field for my project. Can anyone recommend the best approach for achieving this? Here is a snippet of what my multiple select dropdown currently ...

I'm a newbie when it comes to working with streams, but I recently encountered a task that involves downloading a file from an FTP server and then sending it as a stream to an Azure storage

When trying to save data using the Azure stageblock method, I encountered an error stating that the body must be a string, Blob, ArrayBuffer, ArrayBufferView, or a function returning NodeJS.ReadableStream. It seems that FTP sends data in a writable strea ...

How do I set up a slideshow to loop continuously?

I created a slideshow for my website using the script below, but I'm struggling to figure out how to make it repeat. I want the slideshow to go back to the first photo after reaching the last one. Can anyone help please? For reference, please check o ...

Generate QR code scanner by inserting link inside an image tag

Looking to create a QR code scanner using a link URL, but Google APIs are no longer an option since they were deprecated in 2019. Are there any alternative APIs that can achieve the same functionality as GoogleApis? I've tried another API (qr code Api ...

Adding a Resource Bundle to a Vue project

I am looking to integrate the ResourceBundle package into my Vue project. After installing it with the following command: npm install resource-bundle I discovered these exported functions in the index.js file of the ResourceBundle package: export functi ...

Performing Vue CLI Global Upgrade

Struggling to create a new Vue.js project using the Vue CLI service, I encountered an error. With @vue/cli-service 3.0.0-beta.7 installed (confirmed by running vue -V), attempting to start a new project triggered the following error: ...

"Implementing interactive arrow buttons in a horizontal navigation bar using HTML, CSS, and JavaScript

Seeking advice on creating a horizontal navigation bar with arrow buttons at the sides, such as the one shown below. Despite extensive research, I have not found the exact solution I am looking for. This will be implemented in my Vue.js project. |<| P ...

The file extension validation function is not functioning correctly on Windows, however it is successfully working as expected

async upload( @UploadedFile() file: Express.Multer.File, @Body() body: FileUploadDto, ) { const forbiddenExt = [ '.exe', '.bat', ]; const fileName = file.filename || f ...

What is the best way to make a sliding DIV appear at the edge of the browser window?

I recently came across a really interesting effect on a webpage. After arriving on the page, a slide-in window appears after about a minute. It slides in from the right side, remains at 250x180 for around ten seconds, then shrinks to 250x50 and stays visib ...

Distinguishing the clicked stack in a grouped stacked bar chart (using Chart.js) - deciphering the mystery

I have implemented a bar chart similar to the image shown. The chart consists of two stacks grouped together, each stack containing multiple datasets. This implementation involves the use of Vue.js and vue-chartjs. https://i.sstatic.net/OOex2.jpg For han ...

Preventing selection of past dates with Material UI in ReactJS

I'm currently working on a date range picker using react material ui. The goal is to allow users to select a specific date and then disable all past dates from that selected date onward. How can I go about implementing this functionality in react mate ...

Have you heard that according to the jQuery documentation, the .submit() event does not bubble in IE? What implications does this

According to the jQuery documentation for .submit(): In Internet Explorer, the JavaScript submit event does not bubble. However, starting from jQuery 1.4, scripts that use event delegation with the submit event will have consistent behavior across diffe ...

Best practices for securing data when transmitting AJAX requests

I am facing the challenge of escaping text that is being generated via AJAX. If I include a script in the database entry, such as <script>alert('hello world');</script>, it ends up executing a popup on the page. Below is my function ...

Where can I locate the definition of a div in WordPress?

Recently, I installed a new template on my WordPress blog. However, the template designer added a footer copyright that I am unable to delete. It seems like the copyright text is not located in any of the PHP or CSS files. Upon inspecting the elements, I f ...