Как вытащить значение value у элементов option select и использовать его?
При выборе определенного значения должна извлекаться информация из массива, и втавляться в соответствующие ячейки.
Вот пример моего кода:
UPD: изменил код JS для демонстрации кастомного елемнта select который я использую.
var x, i, j, l, ll, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName("custom-select");
l = x.length;
for (i = 0; i < l; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
ll = selElmnt.length;
/*for each element, create a new DIV that will act as the selected item:*/
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/*for each element, create a new DIV that will contain the option list:*/
b = document.createElement("DIV");
b.setAttribute("class", "select-items select-hide");
for (j = 1; j < ll; j++) {
/*for each option in the original select element,
create a new DIV that will act as an option item:*/
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
/*when an item is clicked, update the original select box,
and the selected item:*/
var y, i, k, s, h, sl, yl;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
sl = s.length;
h = this.parentNode.previousSibling;
for (i = 0; i < sl; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("same-as-selected");
yl = y.length;
for (k = 0; k < yl; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "same-as-selected");
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener("click", function(e) {
/*when the select box is clicked, close any other select boxes,
and open/close the current select box:*/
e.stopPropagation();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
});
}
function closeAllSelect(elmnt) {
/*a function that will close all select boxes in the document,
except the current select box:*/
var x, y, i, xl, yl, arrNo = [];
x = document.getElementsByClassName("select-items");
y = document.getElementsByClassName("select-selected");
xl = x.length;
yl = y.length;
for (i = 0; i < yl; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < xl; i++) {
if (arrNo.indexOf(i)) {
x[i].classList.add("select-hide");
}
}
}
/*if the user clicks anywhere outside the select box,
then close all select boxes:*/
document.addEventListener("click", closeAllSelect);
<div class="calc">
<div class="left-side">
<p class="top-txt">вкажіть</p>
<div class="tab-wrapper">
<div class="tab">
<h4 class="value__name">Вік дитини</h4>
<div class="form">
<div class="custom-select">
<form id="formCalc" name="formCalc" method="get">
<select id="myCalc">
<option value="1">1 тиждень</option>
<option value="2">2 тижні</option>
<option value="3">3-4 тижні</option>
<option value="4">2 місяці</option>
<option value="5">3 місяці</option>
<option value="6">4 місяці</option>
<option value="7">5 місяців</option>
<option value="8">6 місяців</option>
<option value="9">7 місяців</option>
<option value="10">8 місяців</option>
<option value="11">9 місяців</option>
<option value="12">10 місяців</option>
<option value="13">11 місяців</option>
<option value="14">12 місяців</option>
<option value="15">14 місяців</option>
<option value="16">16 місяців</option>
<option value="17">20 місяців</option>
</select>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="right-side">
<p class="top-txt">результат</p>
<div class="tab-wrapper">
<div class="tab">
<div class="first-wrapp">
<h3 class="value__name">Кількість на одне годування</h3>
<div class="value__wrapper">
<div class="tab__item">
<h4 class="value__subname">Прокип'ячена<br> вода, мл</h4>
<div class="value" id="water">0</div>
</div>
<div class="tab__item">
<h4 class="value__subname">Кількість мірних<br> ложок порошку</h4>
<div class="value" id="spoons">0</div>
</div>
</div>
</div>
<div class="tab__item">
<h3 class="value__name">Кількість<br> годувань<br> на добу</h3>
<div class="value" id="feedings">0</div>
</div>
<div class="wrapp">
<div class="tab__item product">
<img id="vita1" src="http://localhost/wp-content/uploads/2021/08/vita1.png" alt="Vita Prim 1">
<img id="vita2" src="http://localhost/wp-content/uploads/2021/07/vita2.png" alt="Vita Prim 2">
<img id="vita3" src="http://localhost/wp-content/uploads/2021/07/vita3.png" alt="Vita Prim 3">
</div>
<a href="#" id="calc-btn" class="buy-btn">КУПИТИ</a>
</div>
</div>
</div>
</div>
</div>
Ответы (3 шт):
Для начала я сделал e глобальной переменной, ибо вы обьявляли слушатель событий для undefined, поскольку e была локальной переменной для функции rangeValue, ее значение "не видно" за пределами тела функции.
Дальше вы назначаете слушатель события изменений значения select так:
e.addEventListener("change", rangeValue);
где e - элемент с которым происходит событие,
"change" - название события, rangeValue - имя функции, которая срабатывает при событии.
var e = document.querySelector("#myCalc");
var rangeValue = function() {
var newValue = e.value;
var feed = document.querySelector('#feedings');
var water = document.querySelector('#water');
var spoons = document.querySelector('#spoons');
var data = [
[60, 2, 8, 1],
[90, 3, 7, 1],
[120, 4, 6, 1],
[150, 5, 6, 1],
[180, 6, 5, 1],
[210, 7, 5, 1],
[210, 7, 4, 1],
[210, 7, 4, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[150, 5, 2, 3],
[180, 6, 2, 3],
[210, 7, 2, 3]
];
water.innerHTML = data[newValue][0],
spoons.innerHTML = data[newValue][1],
feed.innerHTML = data[newValue][2];
var vita1 = document.getElementById('vita1');
var vita2 = document.getElementById('vita2');
var vita3 = document.getElementById('vita3');
if (newValue == 1) {
vita1.style.display = "block";
vita2.style.display = "none";
vita3.style.display = "none";
} else if (newValue == 7) {
vita1.style.display = "none";
vita2.style.display = "block";
vita3.style.display = "none";
} else if (newValue == 13) {
vita1.style.display = "none";
vita2.style.display = "none";
vita3.style.display = "block";
}
}
e.addEventListener("change", rangeValue);
<div class="calc">
<div class="left-side">
<p class="top-txt">вкажіть</p>
<div class="tab-wrapper">
<div class="tab">
<h4 class="value__name">Вік дитини</h4>
<div class="form">
<div class="custom-select">
<form id="formCalc" name="formCalc" method="get">
<select id="myCalc">
<option value="1">1 тиждень</option>
<option value="2">2 тижні</option>
<option value="3">3-4 тижні</option>
<option value="4">2 місяці</option>
<option value="5">3 місяці</option>
<option value="6">4 місяці</option>
<option value="7">5 місяців</option>
<option value="8">6 місяців</option>
<option value="9">7 місяців</option>
<option value="10">8 місяців</option>
<option value="11">9 місяців</option>
<option value="12">10 місяців</option>
<option value="13">11 місяців</option>
<option value="14">12 місяців</option>
<option value="15">14 місяців</option>
<option value="16">16 місяців</option>
<option value="17">20 місяців</option>
</select>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="right-side">
<p class="top-txt">результат</p>
<div class="tab-wrapper">
<div class="tab">
<div class="first-wrapp">
<h3 class="value__name">Кількість на одне годування</h3>
<div class="value__wrapper">
<div class="tab__item">
<h4 class="value__subname">Прокип'ячена<br> вода, мл</h4>
<div class="value" id="water">0</div>
</div>
<div class="tab__item">
<h4 class="value__subname">Кількість мірних<br> ложок порошку</h4>
<div class="value" id="spoons">0</div>
</div>
</div>
</div>
<div class="tab__item">
<h3 class="value__name">Кількість<br> годувань<br> на добу</h3>
<div class="value" id="feedings">0</div>
</div>
<div class="wrapp">
<div class="tab__item product">
<img id="vita1" src="http://localhost/wp-content/uploads/2021/08/vita1.png" alt="Vita Prim 1">
<img id="vita2" src="http://localhost/wp-content/uploads/2021/07/vita2.png" alt="Vita Prim 2">
<img id="vita3" src="http://localhost/wp-content/uploads/2021/07/vita3.png" alt="Vita Prim 3">
</div>
<a href="#" id="calc-btn" class="buy-btn">КУПИТИ</a>
</div>
</div>
</div>
</div>
</div>
Ниже пример как считать данные - дальше сможете подставить как нужно, полагаю
let data = [
[60, 2, 8, 1],
[90, 3, 7, 1],
[120, 4, 6, 1],
[150, 5, 6, 1],
[180, 6, 5, 1],
[210, 7, 5, 1],
[210, 7, 4, 1],
[210, 7, 4, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[210, 7, 3, 2],
[150, 5, 2, 3],
[180, 6, 2, 3],
[210, 7, 2, 3]
];
let select_element = document.getElementById('myCalc');
select_element.addEventListener('change', function() {
console.log( data [this.value] );
} );
<div class="calc">
<div class="left-side">
<p class="top-txt">вкажіть</p>
<div class="tab-wrapper">
<div class="tab">
<h4 class="value__name">Вік дитини</h4>
<div class="form">
<div class="custom-select">
<form id="formCalc" name="formCalc" method="get">
<select id="myCalc">
<option value="1">1 тиждень</option>
<option value="2">2 тижні</option>
<option value="3">3-4 тижні</option>
<option value="4">2 місяці</option>
<option value="5">3 місяці</option>
<option value="6">4 місяці</option>
<option value="7">5 місяців</option>
<option value="8">6 місяців</option>
<option value="9">7 місяців</option>
<option value="10">8 місяців</option>
<option value="11">9 місяців</option>
<option value="12">10 місяців</option>
<option value="13">11 місяців</option>
<option value="14">12 місяців</option>
<option value="15">14 місяців</option>
<option value="16">16 місяців</option>
<option value="17">20 місяців</option>
</select>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="right-side">
<p class="top-txt">результат</p>
<div class="tab-wrapper">
<div class="tab">
<div class="first-wrapp">
<h3 class="value__name">Кількість на одне годування</h3>
<div class="value__wrapper">
<div class="tab__item">
<h4 class="value__subname">Прокип'ячена<br> вода, мл</h4>
<div class="value" id="water">0</div>
</div>
<div class="tab__item">
<h4 class="value__subname">Кількість мірних<br> ложок порошку</h4>
<div class="value" id="spoons">0</div>
</div>
</div>
</div>
<div class="tab__item">
<h3 class="value__name">Кількість<br> годувань<br> на добу</h3>
<div class="value" id="feedings">0</div>
</div>
<div class="wrapp">
<div class="tab__item product">
<img id="vita1" src="http://localhost/wp-content/uploads/2021/08/vita1.png" alt="Vita Prim 1">
<img id="vita2" src="http://localhost/wp-content/uploads/2021/07/vita2.png" alt="Vita Prim 2">
<img id="vita3" src="http://localhost/wp-content/uploads/2021/07/vita3.png" alt="Vita Prim 3">
</div>
<a href="#" id="calc-btn" class="buy-btn">КУПИТИ</a>
</div>
</div>
</div>
</div>
</div>
Разобрался, теперь всё работает как надо, всем спасибо!
var x, i, j, l, ll, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName("custom-select");
l = x.length;
for (i = 0; i < l; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
ll = selElmnt.length;
/*for each element, create a new DIV that will act as the selected item:*/
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.setAttribute("id", "select-selected");
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/*for each element, create a new DIV that will contain the option list:*/
b = document.createElement("DIV");
b.setAttribute("class", "select-items select-hide");
for (j = 1; j < ll; j++) {
/*for each option in the original select element,
create a new DIV that will act as an option item:*/
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
/*when an item is clicked, update the original select box,
and the selected item:*/
var y, i, k, s, h, sl, yl;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
sl = s.length;
h = this.parentNode.previousSibling;
for (i = 0; i < sl; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("same-as-selected");
yl = y.length;
for (k = 0; k < yl; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "same-as-selected");
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener("click", function(e) {
/*when the select box is clicked, close any other select boxes,
and open/close the current select box:*/
e.stopPropagation();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
var newValue = document.querySelector("#myCalc").value;
// alert(newValue);
var feed = document.querySelector('#feedings');
var water = document.querySelector('#water');
var spoons = document.querySelector('#spoons');
var data = [
[60, 2, 8],
[90, 3, 7],
[120, 4, 6],
[150, 5, 6],
[180, 6, 5],
[210, 7, 5],
[210, 7, 4],
[210, 7, 4],
[210, 7, 3],
[210, 7, 3],
[210, 7, 3],
[210, 7, 3],
[210, 7, 3],
[210, 7, 3],
[150, 5, 2],
[180, 6, 2],
[210, 7, 2]
];
water.innerHTML = data[newValue][0],
spoons.innerHTML = data[newValue][1],
feed.innerHTML = data[newValue][2];
var vita1 = document.getElementById('vita1');
var vita2 = document.getElementById('vita2');
var vita3 = document.getElementById('vita3');
if (newValue <= 7) {
vita1.style.display = "block";
vita2.style.display = "none";
vita3.style.display = "none";
} else if (newValue <= 13) {
vita1.style.display = "none";
vita2.style.display = "block";
vita3.style.display = "none";
} else if (newValue <= 17) {
vita1.style.display = "none";
vita2.style.display = "none";
vita3.style.display = "block";
}
});
}
function closeAllSelect(elmnt) {
/*a function that will close all select boxes in the document,
except the current select box:*/
var x, y, i, xl, yl, arrNo = [];
x = document.getElementsByClassName("select-items");
y = document.getElementsByClassName("select-selected");
xl = x.length;
yl = y.length;
for (i = 0; i < yl; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < xl; i++) {
if (arrNo.indexOf(i)) {
x[i].classList.add("select-hide");
}
}
}
/*if the user clicks anywhere outside the select box,
then close all select boxes:*/
document.addEventListener("click", closeAllSelect);
.custom-select {
position: relative;
width: 130px;
}
.custom-select select {
display: none;
}
.select-selected {
background-color: #fff;
}
.select-selected:after {
position: absolute;
content: "";
top: 20px;
right: 10px;
width: 0;
height: 0;
border: 6px solid #00b0ca6b;
border-color: #00b0ca transparent transparent transparent;
}
.select-selected.select-arrow-active:after {
border-color: transparent transparent #00b0ca transparent;
top: 13px;
}
.select-items div,
.select-selected {
color: #ffffff;
padding: 8px 16px;
border: 1px solid transparent;
border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
cursor: pointer;
user-select: none;
color: #00b0ca;
font-size: 15px;
}
.select-items:nth-child(1),
.select-selected {
border: 3px solid #00b0ca6b;
border-radius: 17px
}
.select-items {
position: absolute;
background-color: #fff;
top: 100%;
left: 0;
right: 0;
z-index: 99;
}
.select-hide {
display: none;
}
.select-items div:hover,
.same-as-selected {
background-color: rgba(0, 0, 0, 0.1);
}
<div class="calc">
<div class="left-side">
<p class="top-txt">вкажіть</p>
<div class="tab-wrapper">
<div class="tab">
<h4 class="value__name">Вік дитини</h4>
<div class="form">
<div class="custom-select">
<form id="formCalc" name="formCalc" method="get">
<select id="myCalc">
<option value="1">1 тиждень</option>
<option value="2">2 тижні</option>
<option value="3">3-4 тижні</option>
<option value="4">2 місяці</option>
<option value="5">3 місяці</option>
<option value="6">4 місяці</option>
<option value="7">5 місяців</option>
<option value="8">6 місяців</option>
<option value="9">7 місяців</option>
<option value="10">8 місяців</option>
<option value="11">9 місяців</option>
<option value="12">10 місяців</option>
<option value="13">11 місяців</option>
<option value="14">12 місяців</option>
<option value="15">14 місяців</option>
<option value="16">16 місяців</option>
<option value="17">20 місяців</option>
</select>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="right-side">
<p class="top-txt">результат</p>
<div class="tab-wrapper">
<div class="tab">
<div class="first-wrapp">
<h3 class="value__name">Кількість на одне годування</h3>
<div class="value__wrapper">
<div class="tab__item">
<h4 class="value__subname">Прокип'ячена<br> вода, мл</h4>
<div class="value" id="water">0</div>
</div>
<div class="tab__item">
<h4 class="value__subname">Кількість мірних<br> ложок порошку</h4>
<div class="value" id="spoons">0</div>
</div>
</div>
</div>
<div class="tab__item">
<h3 class="value__name">Кількість<br> годувань<br> на добу</h3>
<div class="value" id="feedings">0</div>
</div>
<div class="wrapp">
<div class="tab__item product">
<img id="vita1" src="http://localhost/wp-content/uploads/2021/08/vita1.png" alt="Vita Prim 1">
<img id="vita2" src="http://localhost/wp-content/uploads/2021/07/vita2.png" alt="Vita Prim 2">
<img id="vita3" src="http://localhost/wp-content/uploads/2021/07/vita3.png" alt="Vita Prim 3">
</div>
<a href="#" id="calc-btn" class="buy-btn">КУПИТИ</a>
</div>
</div>
</div>
</div>
</div>