Modifying background with CSS and JavaScript

I am currently facing a dilemma and could really use some fresh perspectives on how to tackle this problem.

var vaction = "{{vaction}}";

  if(vaction === "Driving")
   document.getElementByClassName("cover").style.backgroundImage = url(https://media.nature.com/lw800/magazine-assets/d41586-018-04158-5/d41586-018-04158-5_15590100.gif);
   document.getElementByClassName("table1, table2").style.backgroundColor = 'lime';
  
  else if(vaction === "Sounding The Alarm")
   document.getElementByClassName("cover").style.backgroundImage= url(https://upload.wikimedia.org/wikipedia/commons/e/eb/Blinking_warning.gif);
   document.getElementByClassName("table1, table2").style.backgroundColor = 'orange';
  
  else if(vaction === "Pulling over")
   document.getElementByClassName("cover").style.backgroundImage = url(https://browsifyapp.com/wp-content/uploads/2018/10/model-3-gif-yeah.gif);
   document.getElementByClassName("table1, table2").style.backgroundColor = 'red';
</script>
<style>
table.table2, table.table1 {
width: 190.5px;
height: 50px;
text-align: left;
border-collapse: collapse;
}
table.table1, td, th, table.table2 td, th {
border: 1px solid black;
}
html , body {
    height: 100%;
margin: 0;
padding: 0;
}
div.cover {
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  width: 100%;
  height: 100%;
}
</style>
<div class="cover"> 
  <div>
    <table class="table1">
    <thead><tr><th>Focus</th><th>State</th><th>Alert Level</th></tr></thead>
    <tr><td>Eyes</td><td> {{estate}} </td><td> {{elevel}} </td></tr> 
    <tr><td>Head</td><td> {{hstate}} </td><td> {{hlevel}} </td></tr>
    <tr><td>Body</td><td> {{bstate}} </td><td> {{blevel}} </td></tr>
    <tr><td>Vehicle</td><td> {{vstate}} </td><td> {{vlevel}} </td></tr>
    </table>

    <table class="table2">
    <thead><tr><th>Current action</th></tr></thead>
    <tr><td> {{vaction}} </td></tr>
    </table>
  </div>
</div>

The main concept here is to dynamically adjust the background gif of the cover along with tweaking the colors of table1 and table2 based on the variable supplied. These values are extracted from flaks, so deciding on the aesthetics is my current obstacle.

Answer №1

if(!NodeList.prototype.forEach) // Support for document.querySelectorAll('...').forEach in IE
    NodeList.prototype.forEach = function(method){
        for(var index = 0; index < this.length; index++)
           method(this[index]);
    };
function adjustBackgroundImages(gif_url, color) {
  document.querySelector("div.cover").style.backgroundImage = gif_url;
  document.querySelectorAll(".table1, .table2").forEach(function(table) {
    table.style.background = color;
  });
}
switch("{{action}}") {
  case "Driving":
    adjustBackgroundImages("url(https://media.nature.com/lw800/magazine-assets/d41586-018-04158-5/d41586-018-04158-5_15590100.gif)", "lime");
  break;
  case "Sounding The Alarm":
    adjustBackgroundImages("url(https://upload.wikimedia.org/wikipedia/commons/e/eb/Blinking_warning.gif)", "orange");
  break;
  case "Pulling over":
    adjustBackgroundImages("url(https://browsifyapp.com/wp-content/uploads/2018/10/model-3-gif-yeah.gif)", "red");
  break;
}

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

Arranging multiple lines of text above several images using CSS

I'm encountering issues when trying to place multiple texts in front of multiple images. I've managed to achieve this using absolute and relative positioning, with one text above one image. However, the problem arises when I try to apply this to ...

What are the steps for submitting a form with AJAX?

I am having trouble submitting a form via ajax. I am not receiving any error messages even though I have set up error handling throughout the website. Here is my code: <script type="text/javascript" src="jquery.js"></script> <div id="shower ...

What is the best way to manage the loading and unloading of JavaScript within dynamically loaded partial pages?

Utilizing jQuery and history.js, I manage seamless transitions between partial pages to prevent entire document reloads. Some of these partial pages include unique javascript files. Despite successful page transitions, remnants of executed javascript linge ...

The HTML code is not being styled by Bootstrap

<div class="form-inline"> <div class="form-group "> <label for="txtTitle" class="control-label"> <p class="text-info">Page Title</p> </label> <asp:textbox id="txtTitle" tabindex ...

The system is currently unable to find the specified element

I am facing an issue trying to locate a button that is defined under a specific class using XPATH. The error message "Unable to locate element" keeps popping up. Here are the details of the class: <div class="aui-button-holder inputBtn" id="aui_3_4_0_1 ...

What is the method for submitting a form via input type with htmx?

I am trying to figure out how to send form data when typing into a search input or selecting an item from a dropdown. The form is currently wrapped around both elements, and I need to capture data from both inputs. Any suggestions on how to achieve this ...

Transform a list of H1..6 into a hierarchical structure

I have a task to convert H1 to H6 tags from a markdown file into a JavaScript hierarchy for use in a Table of Contents. The current list is generated by AstroJS and follows this format [{depth: 1, text: 'I am a H1'}, {depth: 2: 'I am a H2}] ...

Implementing Event Listeners for buttons generated dynamically

Currently immersed in an exciting side project and encountered a small hiccup. Essentially, I have a page where users can log in to Spotify. Upon login, they are redirected to a page showcasing their top 10 artists in individual cards, complete with the ar ...

Mastering Angular's ngFor directive allows for powerful manipulation of arrays in the front-end

My goal is to use the last n elements from the orbit array, a task easily achieved with | slice: -n. However, I am facing an issue where in each iteration, I not only need access to the respective item but also its successor. The problem lies in the fact t ...

Request financial data from AlphaVantage using PHP

I'm currently working on utilizing the alphavantage API to retrieve currency exchange information. In order to obtain the desired data, I am using the following query URI: https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_cu ...

The height of the TinyMCE editor refuses to adjust

Hello, I'm new to utilizing Tinymce within an AngularJS environment. Specifically, I am working with the Tinymce library version 4.3.13. I have constructed some HTML and angularJS code to implement the Tinymce editor, but I've encountered a probl ...

Verify if any choices are available before displaying the div block

I need to determine if there is a specific option selected in a dropdown menu, and then display a div if the option exists, otherwise hide it. I'm not just checking the currently selected option, but all available options. if (jQuery(".sd select opti ...

What is the best way to align a div to the bottom of its container div in Bootstrap 5?

Within the Bootstrap 5 code snippet provided, there is a nested div with the ID "div-in-right-div" inside another div with the ID "right-div". Currently, "div-in-right-div" is aligned with the top of "right-div". The question arises: How can you adjust the ...

What are the distinguishing features between UMD and CommonJS (CJS) package directories, and which one is preferable for my use?

After setting up my package.json, I successfully installed reactjs and react-dom. "dependencies": { "bootstrap": "^v4.1.1", "popper.js": "^1.14.3", "react": "^v16. ...

Unlimited scrolling feature implemented in a specified container using AngularJS

Does anyone have experience with using angularjs infinite scroll within an inner DIV instead of just the browser window? I'm trying to implement infinite scroll within a specific content wrapper that has its own scrollbar. The main page is fixed and ...

Run a PHP script on the current page upon choosing an option from a dropdown list with the help of Ajax or JavaScript

I'm currently working on a MySQL query that needs to be executed when a user selects options from multiple dropdown lists. What I am looking for is the ability to automatically run a query related to the selected dropdown list option using AJAX/JavaS ...

Identifying when an element hovers over a particular section

I am facing an issue with the side dot navigation on my website. The navigation is standard with a fixed position, but I also have two types of sections with different backgrounds - one white and the other black. The problem arises when the dots are not vi ...

How to maintain the focus within a jQuery dialog box

Exploring the world of jQuery dialog, I'm eager to incorporate it into my latest side project. My goal is to enhance accessibility by adding tabindex to the divs within the dialog for easy tab navigation. However, I encountered an issue where the focu ...

The anchorEl state in Material UI Popper is having trouble updating

I am currently facing an issue with the Material UI popper as the anchorEl state remains stuck at null. Although Material UI provides an example using a functional component, I am working with a class-based component where the logic is quite similar. I w ...

My CSS nesting seems to be posing a problem - any idea what

Something unexpected happened while I was working on a project with others. Last night, some files were edited and when I updated my subversion today, the nested divs seemed to have lost their hierarchy. I have been trying all day to fix this issue but wit ...