What is the process of linking a menu to a screen with Javascript?

Looking to link my HTML list to a JavaScript display in a similar way as shown in this gif: . Below is the HTML and JS code I have:

<label for="Villes-select">Choose a city:</label>
    <select onchange="showCity()" ; name="Cities" id="Villes-select" > ;
    <option value="">--Select an option--</option>
    <option value="Paris">Paris</option>
    <option value="Lyon">Lyon</option>
    <option value="Geneva">Geneva</option>
</select>

   <h1> <strong> Weather TP </strong> </h1>
  <p> This site displays weather information of cities whenever you want it.</p>
  <div class="flex">
  
     
    <div class="city" id="Paris"> <h2> Paris </h2>
  <p> -1°C <img src="C:/Users/Dell/Downloads/Icon HTML/Thermo.png" alt="image1" height="25" width="25"/> <img src="C:/Users/Dell/Downloads/Icon HTML/Paris.png" alt="image2" height="25" width="25"/> </p> </div>
    <div class="city" id="Lyon"> <h2> Lyon </h2>
<p> 0°C <img src="C:/Users/Dell/Downloads/Icon HTML/Thermo.png" alt="image1" height="25" width="25"/> <img src="C:/Users/Dell/Downloads/Icon HTML/SunCloud.png" alt="image3" height="25" width="25"/> </p> </div>   
    <div class="city" id="Geneva"> <h2> Geneva </h2>
<p> 6°C <img src="C:/Users/Dell/Downloads/Icon HTML/Thermo.png" alt="image1" height="25" width="25"/> <img src="C:/Users/Dell/Downloads/Icon HTML/SunIcon.png" alt="image4" height="25" width="25"/> </p> </div>
    </div>

function showCity(){
var elements = document.querySelectorAll('.city');
for (var i=0 ; i< elements.length ; i++){
    var element = elements[i];
}
let selectedCity = document.getElementById('Villes-select');
let cities = document.getElementsByClassName('city');
for(var i=0; i < 1  ; ++i){
    if(cities[i]= selectedCity){
        element.style.display= "none";
    }
    else{
        element.style.display= "";
    }
}   

}

Answer №1

After reviewing your code, I have identified several issues that needed fixing in order to improve its functionality. The updated code provided below should help you get closer to the desired outcome.

function showCity(){
  var cities = document.querySelectorAll('.city');
  let selectedCity = document.getElementById('Cities-select').value;

  for(var i=0; i < cities.length; ++i){
    if(cities[i].id == selectedCity){
        cities[i].style.display = "";
    }
    else{
        cities[i].style.display = "none";
    }
  }   
}
<label for="Cities-select">Select a city:</label>
    <select onchange="showCity()" ; name="Cities" id="Cities-select" >
    <option value="">--Select an option--</option>
    <option value="Paris">Paris</option>
    <option value="Lyon">Lyon</option>
    <option value="Geneva">Geneva</option>
</select>

   <h1> <strong> Weather App </strong> </h1>
  <p> This site displays the weather of different cities based on user selection.</p>
  <div class="flex">
  
     
    <div class="city" id="Paris"> <h2> Paris </h2>
  <p> -1°C <img src="#" alt="weather icon" height="25" width="25"/> <img src="#" alt="city image" height="25" width="25"/> </p> </div>
    <div class="city" id="Lyon"> <h2> Lyon </h2>
<p> 0°C <img src="#" alt="weather icon" height="25" width="25"/> <img src="#" alt="city image" height="25" width="25"/> </p> </div>   
    <div class="city" id="Geneva"> <h2> Geneva </h2>
<p> 6°C <img src="#" alt="weather icon" height="25" width="25"/> <img src="#" alt="city image" height="25" width="25"/> </p> </div>
    </div>

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

Is there a way to automate clicking a button when an ajax request is successful through programming?

I am currently working on implementing an ajax cart feature for an e-commerce website. The specific functionality I am aiming to achieve is that when a user clicks the 'add to cart' button, the item is added to the cart and then the user is autom ...

Tips for Maximizing the Efficiency of a Search in a Complexly Nested Object

I am faced with a complex JavaScript object structure that represents hierarchical data. This object contains multiple levels of nested children, and I have the task of searching for a specific value within this intricate structure. Let's take a look ...

Why is useEffect being executed twice?

For some reason, when I try to run useEffect for the first time page load, it ends up running twice. I can't seem to figure out why this is happening. Can someone please provide assistance? I'm currently using React 18 and I need help understand ...

Animating 3D shapes in Three.js

I developed a futuristic snake game where snake-like entities move around in an animated fashion using various cube meshes and the THREE.BoxGeometry: https://i.sstatic.net/GVDNj.png To enhance the game, I aim to create each snake with a single mesh and g ...

Encountering an error message stating "WebResource.axd?d=xxxxxxx Uncaught TypeError: Cannot assign value to undefined property."

In my project using asp.net, I encountered an error message when clicking on a specific asp.net Button. The error displayed was: WebResource.axd?d=xxxxx Uncaught TypeError: Cannot set property 'value' of undefined at HTMLFormElemen ...

Unable to retrieve variable declared externally from Switch Statement

Having an issue with assigning a value to the variable contactNumber inside a switch statement. Upon reaching the assignment line, an error SyntaxError: Unexpected identifier is triggered within the switch statement context. function contactMessageForCo ...

Detecting when an image, passed through props, finishes loading and updating the state in React - here's how!

Is it possible to display a different image (a fake avatar) while the final avatar image is loading? I want to detect when the prop image is loaded and change a state. Any ideas on how to achieve this? Thank you! class ImageUser extends React.Component ...

Convert Excel data to JSON by extracting information from specific columns

Attempting to extract JSON values from a table. The JSON is currently generated as rows from Excel. Looking to parse the Excel data column by column instead. Seeking assistance with this issue. Utilizing Angular for the parsing process. Upload() { ...

Using HTML and CSS, design a customizable tick box without explicitly utilizing the 'checkbox' element

Check out this code snippet (and yes, I'm using a basic reset.css): .checkbox { border: 1px solid black; width: 10px; height: 10px; } <ul> <li> <p><div class="checkbox"></div>I agree!</p> </li&g ...

problem encountered while rendering the React Native flatList

Hello fellow developers, I'm currently working on creating a RssFeeder app to enhance my skills in React Native. Unfortunately, I've encountered an issue while trying to render objects obtained from the newsAPI in a flatList. The data is not disp ...

What is the process by which browsers manage AJAX requests when they are made across

I have encountered an issue that is puzzling to me, and I suspect it might be due to my misunderstanding of how the browser handles AJAX requests. Just for context, I am using Codeigniter on an Apache server and triggering AJAX requests with jQuery. The b ...

Ensure that each child component receives a prop

Can you transfer a prop with a function to any child component? Specifically, I want to send a function from a parent component to each {children} element: <> <Navbar /> <main>{children}</main> <Footer /> </&g ...

Utilize a standard JavaScript function from a separate document within a function of a React component

I am facing an issue where I need to incorporate a standard JavaScript function within a React component function. The script tags are arranged in the footer in the following order: <script src="NORMAL JAVASCRIPT STUFF"></script> // function ...

Issue with displaying Facebook meta image when sharing

Having trouble displaying a custom Facebook image on the share button. I am testing from localhost, could that be the issue (unable to test in production yet)? The image address is sourced from the web and I have tried various options. <meta property=" ...

Discovering information within an array in MongoDB

Here is a sample collection: { "name":"silver", mywants:[ {"_id":objid(545454ddfdf5),mark:{"english":100,"math":100,"science":100}}, {"_id":objid(5878784dfd5d),mark:{"english":100,"math":100,"science":100}}, {"_id":objid(5454dfd44545), ...

Loading gltf files with Three.js does not automatically update external variables

When I import a gltf object, it seems to render in the browser but I am unable to access it using an outside variable. What could be causing this issue? let loadedModel; gltfLoader.load('./assets/javaLogo.gltf', function(gltf){ loadedModel = ...

The dichotomy between public and private methods within a Vue.js component

Within a component, I have two functions defined. One is foo(), which is defined within <script>, and the other is fooExported(), which is defined in the body of export default {}. My understanding is that functions inside export default {} can be a ...

Always keep this checkbox checked and only uncheck the others when necessary

Whenever I check a checkbox and then click on another checkbox, I don't want the initially checked checkbox to be unchecked, but rather a different one... For example: If I check checkbox 1, only checkbox 1 is checked; If I check checkbox 2, both ...

How can Vuetify components be imported separately in the right way?

I’m currently getting acquainted with Vuetify and I’m finding it quite challenging to figure out how to import a specific component for use. My current version of Vuetify is 2.4.2 According to the documentation, they mentioned about the path to vueti ...

Unsure why my React component isn't triggering a re-render?

I encountered an issue when trying to update my component based on a state change. When I update the state outside of an HTTP call, the component updates correctly. However, when I try to do the same inside an HTTP get call, the state is updated but the ...