I'm trying to work with JavaScript for the first time. I have gotten a dropdown list to do what I wanted thanks to help on this forum:
$('select').change(function(){
const options = $("option");
let selectedOption = "";
let directDisplayString = "";
options.each(function(index){
let option = options[index];
if($(option).prop("selected")) {
selectedOption = $(option).val();
};
if(selectedOption === "450 Litre") {
directDisplayString = '450 Litre Direct Storage Vessel with:';
};
if(selectedOption === "550 Litre") {
directDisplayString = '550 Litre Direct Storage Vessel with:';
};
if(selectedOption === "900 Litre") {
directDisplayString = '900 Litre Direct Storage Vessel with:';
};
});
$("#directChoiceDisplay").empty().append(directDisplayString);
});
Now, I have dropdown lists on multiple webpages on my site. I wanted to do a similar thing for each page. When I have added more script to my JavaScript file, the second bit of code doesn't seem to execute:
$('select').change(function(){
const options2 = $("option");
let selectedOption2 = "";
let stainlessDisplayString = "";
options2.each(function(index){
let option2 = options2[index];
if($(option2).prop("selected")) {
selectedOption2 = $(option2).val();
};
if(selectedOption2 === "3 kW 385 mm") {
stainlessDisplayString = '3 kW 385mm immersed 2 1/4 BSP 1/3 Phase 240/415v';
};
if(selectedOption2 === "6 kW 385 mm") {
stainlessDisplayString = '6 kW 385mm immersed 2 1/4 BSP 1/3 Phase 240/415v';
};
if(selectedOption2 === "9 kW 410 mm") {
stainlessDisplayString = '9 kW 410mm immersed 2 1/4 BSP 1/3 Phase 240/415v';
};
if(selectedOption2 === "12 kW 585 mm") {
stainlessDisplayString = '12 kW 585mm immersed 2 1/4 BSP 3 Phase 415v';
};
});
$("#stainlessChoiceDisplay").empty().append(stainlessDisplayString);
});
I'm guessing my problem is ambiguity in the names of the different dropdown lists or something like that. I've tried to play around with names to fix it, but to no avail.
Any help would be greatly appreciated.
A test version of the webpage that is not working is here.
(A version of the page that IS working is here).
console? Is there more than one<select>element within the HTMLdocument?$('select').change?