Utilizing JavaScript for manipulating arrays and displaying images

After asking this question previously without a satisfactory solution, I am hoping to provide better clarification. Imagine having an array with 3 items and it lands on 0 - a code is set up to display this in a div. Now, I want the image to be shown right next to the displayed array. For instance, if the array consists of BMW, Civic, and Mercedes, and a random number generator selects 1 (BMW), the word will appear in the div and a BMW logo should appear next to it. The same goes for Civic and Mercedes, each having their respective logos displayed alongside their names. Additionally, I would like to add IDs so that CSS background images can be easily added according to the picture's design.

Answer №1

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Car Selector</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

  <ul id="carList">


  </ul>

  <button id="getRandomCar">Get Random Car</button>


</body>
  <script>
    $(document).ready( function(){
    var vehicles = [{
     brand : 'Toyota',
     logo : 'toyota-logo.png',
     model : 'Camry',
    },
    {
     brand : 'Ford',
     logo : 'ford-logo.png',
     model : 'Mustang',
    },
    {
     brand : 'Chevrolet',
     logo : 'chevy-logo.png',
     model : 'Silverado',
    }];

function randomNumGen(limit) {
  return Math.round(Math.random()*limit); 
}

function displayRandomCar() {
  $('#getRandomCar').click( function() {

    var carIndex = randomNumGen(vehicles.length - 1);
    console.log(carIndex);
    var template = '<li>';
    template += 'Brand: ' + vehicles[carIndex].brand + '</br>';
    template += 'Logo: ' + vehicles[carIndex].logo + '</br>';
    template += 'Model: ' + vehicles[carIndex].model + '</br>';

    $('#carList').html(template);

  } );
}
        displayRandomCar();
    });
  </script>
</html>

For more details, visit this link.

An array of objects (vehicles) has been created to store different car information.

The script generates a random number to select a car from the vehicles array and displays its details in the DOM.

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

Can you explain the contrast between onsubmit="submitForm();" and onsubmit="return submitForm();"?

Is it possible that the form below is causing double submissions? <form name="myForm" action="demo_form.asp" onsubmit="submitForm();" method="post"> function submitForm(){ document.myForm.submit(); } I've noticed a bug where sometimes two ...

Issues with displaying HTML video content

Seeking assistance with a unique coding predicament. I've got an HTML file that showcases a JavaScript-powered clock, but now I'm looking to enhance it by incorporating a looped video beneath the time display. Oddly enough, when the clock feature ...

Is it possible to compare every element in an array with one another using an asynchronous process in NodeJS?

I am currently utilizing Resemble.js for image comparison within my web application. I have an array of image URLs that I need to compare with each other in order to calculate a unique score for each image. For example: [url1, url2, url3, url4] The minimu ...

The battle between Iteration and Recursion: Determining the position of a point in a sequence based

One of the challenges I'm facing involves a recursive function that takes a point labeled {x,y} and then calculates the next point in the sequence, recursively. The function in question has the following structure: var DECAY = 0.75; var LENGTH = 150 ...

extraction of information from an object within an array

I am working with an array of objects containing certain keys and values. My goal is to organize these objects by key and then display all the values associated with each key together. [ { '18': 'x' }, { '17': 'y' ...

How can I use JavaScript to disable a table row and then save the selected option in a MySQL database?

I have a PHP snippet that dynamically displays table rows. Each row contains a radio button with "Yes" and "No" options. I have implemented a JS function where, upon choosing an option, a pop-up box is displayed. If the user selects the "Yes" option in t ...

Troubleshooting: jQuery click event not functioning correctly with if-else statement

For some reason, I can't seem to figure out how to toggle the color of all list items with a class of "link" between blue and black. I've searched through numerous posts but still can't find a solution. Working Example: http://jsfiddle.net/ ...

How to center an image vertically inside a link using CSS

My goal is to vertically align an image inside an anchor element using the following code: <ul class="thumbnails"> <li class="span2"> <a href="#" class="thumbnail"> <img src="http://www.forodefotos.com/attachme ...

Prevent a <span> element from affecting the linking functionality of an <a> tag

Is it possible to make a hyperlink clickable without including the <span> tags within it and instead attaching a jQuery event to those tags? This is the code I'm using. It utilizes bootstrap's list-group for a navigation element: <ul cl ...

Configuring Jest unit testing with Quasar-Framework version 0.15

Previously, my Jest tests were functioning properly with Quasar version 0.14. Currently, some simple tests and all snapshot-tests are passing but I am encountering issues with certain tests, resulting in the following errors: console.error node_modules/vu ...

Implementing a React app mounting functionality upon clicking an HTML button

Is there a way to mount a react application by clicking on a standard html button outside of the application itself? My index.html layout is as follows: <div id="app"></div> <button id="button-root">Live chat</butt ...

What is the process of importing an HTML template into a Vue.js project?

I downloaded a template from the internet that I want to import into my vue project. This template includes HTML, CSS, and Javascript files of its own. How can I utilize vue-router to access the index.html file in this template without converting them into ...

Unbinding or undoing an 'onclick' event when another 'onclick' event is triggered

I am facing an issue where clicking on elements with 'onclick' functions works as expected, but when I click on a different element with another 'onclick' function, the first one remains active. What I actually want is for the previous ...

Encase React black box block components and incorporate DOM event listeners

In my React component, I am using a standard interface to wrap various other components that act as "blackbox" plugins, since I may not have authored them. These plugins must meet certain requirements outlined in the interface to work within the plugin wra ...

How do you set a background image for the color of the font?

Imagine I have this piece of code: <span>Hello world!</span> Along with the following CSS: span{ color:red; } Is it possible to replace the red value with an image? Something like url(images/text-bg.png);? I am looking to add a texture to m ...

How can I retrieve and showcase the size of each file, including images, CSS, and JS, present on a website?

Currently, my goal is to create a dashboard capable of retrieving the file size for all resources (images, javascript, css, etc.) loaded on a webpage. I aim to then further filter these resources by file type and file size in order to identify which ones c ...

What is the process of importing an IIFE-based JavaScript module into an Angular TypeScript application?

I have come across a third-party SDK that is structured as an oldschool IIFE based module. The code looks something like this: var ThirdPartySDK = (function() { var export = {}; // Adding some methods to export return export; })(); To use this SD ...

Exploring the functions of JointJS within a Node.js environment

I am a beginner in using JavaScript, JointJS, and Node.js. I am currently working on a Node.js project in WebStorm, and the file structure looks like this: /test /bin www /node_modules /public /routes index.js users.js /views error.jade ...

What is the best way to click on a specific webElement in a group of webElements using Python Selenium?

After discovering how to locate a subelement of an element using Selenium in python through this informative Stack Overflow post, I am now eager to take my skills to the next level. Selenium Get WebElement inside a WebElement My goal is to be able to exe ...

New properties are not being successfully added to Passport Profiles

I am currently utilizing Passport OAuth2.0 with Google as my provider and encountering an unusual syntax issue. The structure of my authentication setup is as follows: passport.use(new GoogleStrategy({ clientID: GOOGLE_CLIENT_ID, clientSecret: GO ...