What is the best way to fill out a secondary drop

Is it possible to dynamically populate a secondary selection based on the User's choice in the first selection?

For example, if the user selects "Silver," how can I automatically remove "Silver" from the second selection options?

var b = {
  img: "https://elo-boost.net/images/tiers/bronze_5.png",
  pret: 10
};
var s = {
  img: "https://elo-boost.net/images/tiers/silver_5.png",
  pret: 20
};
var g = {
  img: "https://elo-boost.net/images/tiers/gold_5.png",
  pret: 30
};
var p = {
  img: "https://elo-boost.net/images/tiers/platinum_5.png",
  pret: 40
};
var d = {
  img: "https://elo-boost.net/images/tiers/diamond_5.png",
  pret: 50
};
var m = {
  img: "https://elo-boost.net/images/tiers/master_1.png",
  pret: 60
};

var select1 = document.getElementById("sel1");
var select2 = document.getElementById("sel2");
var imgleft = document.getElementById("limg");
var imageright = document.getElementById("rimg");
var pret1 = document.getElementById("pret1");
var pret2 = document.getElementById("pret2");

var sm = document.querySelector("h1");

function sum() {
  var ne = document.querySelector("#pret1").innerHTML;
  var wo = document.querySelector("#pret2").innerHTML;
  sm.innerHTML = parseInt(ne) + parseInt(wo);
}

// Event listener on select1

select1.addEventListener("change", function() {
  if (this.value == 1) {
    imgleft.src = b.img;
    pret1.textContent = b.pret;
  }
  if (this.value == 2) {
    imgleft.src = s.img;
    pret1.textContent = s.pret;
  }
  if (this.value == 3) {
    imgleft.src = g.img;
    pret1.textContent = g.pret;
  }
  if (this.value == 4) {
    imgleft.src = p.img;
    pret1.textContent = p.pret;
  }
  if (this.value == 5) {
    imgleft.src = d.img;
    pret1.textContent = d.pret;
  }
  sum();
});

// Event listener on select2

select2.addEventListener("change", function() {
  if (this.value == 1) {
    imageright.src = b.img;
    pret2.textContent = b.pret;
  }
  if (this.value == 2) {
    imageright.src = s.img;
    pret2.textContent = s.pret;
  }
  if (this.value == 3) {
    imageright.src = g.img;
    pret2.textContent = g.pret;
  }
  if (this.value == 4) {
    imageright.src = p.img;
    pret2.textContent = p.pret;
  }
  if (this.value == 5) {
    imageright.src = d.img;
    pret2.textContent = d.pret;
  }
  sum();
});
<div style="float:left; width: 50%;">
  <div>
    <img src="https://elo-boost.net/images/tiers/silver_5.png" id="limg">
    <p id="pret1" style="display:none;"></p>
  </div>
  <select id="sel1" class="slt1">
    <option value="0">current rank</option>
    <option value="1">BRONZE</option>
    <option value="2">SILVER</option>
    <option value="3">GOLD</option> 
    <option value="4">PLATINUM</option>  
    <option value="5">DIAMOND</option>
  </select>
</div>


<div style="float:left; width: 50%;">
  <div>
    <img src="https://elo-boost.net/images/tiers/silver_5.png" id="rimg">
    <p id="pret2" style="display:none;"></p>
  </div>
  <select id="sel2" class="slt2">
    <option value="0">desired rank</option>
    <option value="1">BRONZE</option>
    <option value="2">SILVER</option>
    <option value="3">GOLD</option> 
    <option value="4">PLATINUM</option>  
    <option value="5">DIAMOND</option>
  </select>
</div>
<h1>Sum</h1>

Answer №1

If you're looking for a snippet, I believe this is what you need. (I'm not very familiar with LOL, so the ranks may be incorrect)

var ranks = {
    Bronze: ["Silver", "Gold", "Platinum", "Diamond"],
    Silver: ["Gold", "Platinum", "Diamond"],
    Gold: ["Platinum", "Diamond"],
    Platinum: ["Diamond"],
    Diamond: ["Diamond"]
}

function desireRank(value) {
    if (value.length == 0) document.getElementById("desiredRank").innerHTML = "<option></option>";
    else {
        var catOptions = "";
        for (desiredRankId in ranks[value]) {
            catOptions += "<option>" + ranks[value][desiredRankId] + "</option>";
        }
        document.getElementById("desiredRank").innerHTML = catOptions;
    }
}
<select name="currentRank" id="currentRank" onChange="desireRank(this.value);">
    <option value="" disabled selected>Select</option>
    <option value="Bronze">Bronze</option>
    <option value="Silver">Silver</option>
    <option value="Gold">Gold</option>
    <option value="Platinum">Platinum</option>
    <option value="Diamond">Diamond</option>
</select>
<select name="desiredRank" id="desiredRank">
    <option value="" disabled selected>Select</option>
</select>

Answer №2

I've designed an Object containing the names, generateOptions is a function that retrieves the badges list as an array of strings (retrieved using Object.keys) and creates an option for each string.

updateSelect is a function that takes a select element to update and the value to exclude. It generates the array of strings and filters out the excluded value, then generates the new options.

const badges = {
  bronze: {
    img: "https://elo-boost.net/images/tiers/bronze_5.png",
    price: 10
  },
  silver: {
    img: "https://elo-boost.net/images/tiers/silver_5.png",
    price: 20
  },
  gold: {
    img: "https://elo-boost.net/images/tiers/gold_5.png",
    price: 30
  },
  platinum: {
    img: "https://elo-boost.net/images/tiers/platinum_5.png",
    price: 40
  },
  diamond: {
    img: "https://elo-boost.net/images/tiers/diamond_5.png",
    price: 50
  },
  master: {
    img: "https://elo-boost.net/images/tiers/master_1.png",
    price: 60
  }
};

var select1 = document.getElementById("sel1");
var select2 = document.getElementById("sel2");
var imgleft = document.getElementById("limg");
var imageright = document.getElementById("rimg");
var price1 = document.getElementById("price1");
var price2 = document.getElementById("price2");

var total = document.querySelector("h1");

function calculateTotalPrice() {
  total.innerHTML = badges[select1.value].price + badges[select2.value].price;
}

// eventListener on select1

function generateOptions(select, badges) {
  select.options.length = 0;
  badges.forEach((type, index) => {
    select.options[index] = new Option(type, type);
  })
}

function updateSelect(select, otherSelectValue) {
  const badgesKeys = Object.keys(badges).filter(key => key !== otherSelectValue);

  generateOptions(select, badgesKeys);

}

generateOptions(select1, Object.keys(badges));
generateOptions(select2, Object.keys(badges));

select1.addEventListener("change", function() {
  const option = badges[this.value];
  imgleft.src = option.img;
  price1.textContent = option.price;
  calculateTotalPrice();
  updateSelect(select2, this.value);
});

// eventListener on select2

select2.addEventListener("change", function() {
  const option = badges[this.value];
  imageright.src = option.img;
  price2.textContent = option.price;
  calculateTotalPrice();
  updateSelect(select1, this.value);
});
<div style="float:left; width: 50%;">
  <div>
    <img src="https://elo-boost.net/images/tiers/silver_5.png" id="limg">
    <p id="price1" style="display:none;"></p>
  </div>
  <select id="sel1" class="slt1"></select>
</div>


<div style="float:left; width: 50%;">
  <div>
    <img src="https://elo-boost.net/images/tiers/silver_5.png" id="rimg">
    <p id="price2" style="display:none;"></p>
  </div>
  <select id="sel2" class="slt2"></select>
</div>
<h1>Total Price</h1>

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

Troubleshooting Typescript compilation using tsc with a locally linked node package

In our project using React/React Native with Typescript, we have a mobile app built with React Native and a shared private package that is utilized by both the React Native client and the React frontend. Due to frequent changes in the shared package, we a ...

Angular JavaScript Object Notation structure

I am a beginner in AngularJS and I'm attempting to create formatted JSON based on the values of table rows (tr) and cells (td). The table rows are generated automatically. When the form is submitted, I try to create the JSON values. Once the form is ...

Create a layout using CSS that features two columns. The left column should be fluid, expanding to fill all remaining space, while the right column should be fixed

I need to ensure that the Online Users div always remains at a fixed size of 200px, while allowing the chat window next to it to resize dynamically based on available space. Essentially, I want the chat window to adjust its width as the window is resized, ...

I'm looking to extract various data from a SQLite table using the URL with ExpressJS. What's the best way to achieve

I need to access data from a SQLite database table that contains information on accounts, movies, and reviews. Specifically, the structure of the reviews-table is as follows: CREATE TABLE IF NOT EXISTS reviews ( id INTEGER PRIMARY KEY, authorId INT ...

What are the steps to create two frames using Twitter Bootstrap?

I'm just starting to work with Twitter Bootstrap and I need some help. Can someone assist me in creating a two-column layout inside the HTML, where the menu in the header stays visible even when scrolled down? I've included my HTML code below. Th ...

Dealing with a divided image in a separate thread: What you need to know

I am facing a challenge with incorporating a LARGE image into a website, which is affecting performance. My solution is to divide the image into smaller pieces and stitch them together using a grid upon loading. The only issue is that it must be in the ba ...

Exploring the dynamic data loading feature in Vue 3 by fetching data from the server and displaying it using a v-for

I am encountering an issue where I want to display data dynamically from a database using a v-for loop. However, when I attempt to push new data into the array, they show correctly in a console.log() but do not reflect any changes in the template. I have ...

Execution of the PHP code within the div element with the id 'upload' is not working properly when trying to use xhr.open("POST", $id("upload").action, true)

Within my .js file, I came across the following line: xhr.open("POST", $id("upload").action, true); xhr.setRequestHeader("X_FILENAME", file.name); xhr.send(file); In the index.php file, there is a form structured like this: <form id="upload" action=" ...

Attempting to create a slider utilizing jQuery

I'm currently working on creating a slider using jquery. I have downloaded the cycle plugin for the slider and included it in my file. The slider consists of 7 pictures. Below is the code I am using, can someone please help me identify any issues? &l ...

When using jQuery to enable contenthover on divs, they will now start a new line instead of

I've been working on achieving a layout similar to this, with the contenthover script in action: Mockup Draft Of Desired Look However, the result I'm getting is different from what I expected, it can be seen here. The images are not aligning co ...

Experiencing problems with the Locale setting when utilizing the formatNumber function in Angular's core functionalities

I am having trouble formatting a number in Angular using the formatNumber function from the Angular documentation. Here is my code snippet: import {formatNumber} from '@angular/common'; var testNumber = 123456.23; var x = formatNumber(Numb ...

Currently, I am attempting to retrieve text input through the use of AngularJS

Having trouble retrieving text input values using Angular JS? The console keeps showing undefined. <div ng-controller="favouritesController" class="col-xs-12 favList"> <input type="text" ng-model="newFav" ng-keyup= "add($event)" class="col-xs-1 ...

Experiencing a problem with JQuery Offset when a fixed position is applied to a

My website has a page layout and style similar to the example provided in this JsFiddle. When I use JQuery to click on a button, the content is displayed correctly as shown below: https://i.sstatic.net/V89qa.jpg However, if I scroll down the page first ...

The functionality of angular-ui's ui-utils and ui-scroll module is currently nonfunctional in version 0.1.0

I have been trying to implement the features from this Angular UI library: http://angular-ui.github.io/ui-utils/, particularly focusing on this aspect: https://github.com/angular-ui/ui-utils/blob/master/modules/scroll/README.md Unfortunately, despite my e ...

sending a selection of JSON string values to a jQuery function

I have a JSON string that contains different items, but I am only interested in the 'locked' array and would like to pass it to a jQuery function. The JSON string was generated using json_encode from PHP. {"ignored":[],"status":{"message":"succe ...

Upon transitioning from typescript to javascript

I attempted to clarify my confusion about TypeScript, but I'm still struggling to explain it well. From my understanding, TypeScript is a strict syntactical superset of JavaScript that enhances our code by allowing us to use different types to define ...

CSS - I add space between the components of the layout, but the color of the space does not match the body color

I am aiming for my website layout to have distinct margins between the left and right side of the content, as well as between the header and navbar. However, I do not want these spaces' colors to change to match the body's background color. http ...

Convert Screen Coordinates to World Coordinates with ThreeJS

While many people are interested in converting camera position to screen position, my question is how to achieve the opposite. Currently, I am trying to set the position of the "door" as a percentage of the screen, with the necessary calculations already ...

Stopping videos in the JQuery Cycle Plugin triggers the stop event

Currently, I have a simple cycle set up with YouTube videos using the following code: <script type="text/javascript"> $(document).ready(function () { $('#cycle').cycle({ fx: 'fade', sp ...

The first three AJAX requests are successful, but the fourth one fails to reach the PHP script

I am currently working on cascaded selects (a total of 4) that retrieve data from a database. To populate them, I use SQL queries based on the selection made in the previous select element. To establish communication between the select element and the subs ...