Guys, please tell me how to implement such a thing, there is 1 select and 1 input there number, when you select something from the select and enter the number in the input, for example, when you select a banana, the banana price multiplies by the quantity, and the result is displayed slightly lower

<form> <select> <option value="Banan">Π‘Π°Π½Π°Π½</option> <option value="Apple">Π―Π±Π»ΠΎΠΊΠΎ</option> <option value="Pear">Π“Ρ€ΡƒΡˆΠ°</option> <option value="Grapes">Π’ΠΈΠ½ΠΎΠ³Ρ€Π°Π΄</option> </select> <input type="number" value="amount" min="1" max="50" step="1" placeholder="Π£ΠΊΠ°ΠΆΠΈΡ‚Π΅ ΠΏΡ€Π΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ количСство (ΠΊΠ³)"> </form> <div>Π—Π΄Π΅ΡΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ умноТСния</div> 

  • Formulate the right question, nothing is clear! Where is the price, where is the quantity? - Ihor Tkachuk

3 answers 3

In order not to upset the teacher of mathematics and not to multiply kilograms by bananas, you must still add the price to each option, and then multiply.

 var fruit = document.getElementById('fruit'), weight = document.getElementById('weight'), result = document.getElementById('result'); fruit.addEventListener('change', function() { listener(fruit); }); weight.addEventListener('change', function() { listener(fruit); }); function listener(elem) { var price = elem.querySelector(':checked').getAttribute('data-price'), kg = weight.value; result.innerHTML = multiply(price, kg); } function multiply(a, b) { return a * b; } 
 <form> <select id="fruit"> <option value="Banan" data-price="100" selected>Π‘Π°Π½Π°Π½</option> <option value="Apple" data-price="150">Π―Π±Π»ΠΎΠΊΠΎ</option> <option value="Pear" data-price="200">Π“Ρ€ΡƒΡˆΠ°</option> <option value="Grapes" data-price="250">Π’ΠΈΠ½ΠΎΠ³Ρ€Π°Π΄</option> </select> <input id="weight" type="number" value="amount" min="1" max="50" step="1" placeholder="Π£ΠΊΠ°ΠΆΠΈΡ‚Π΅ ΠΏΡ€Π΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ количСство (ΠΊΠ³)"> </form> <div id="result">Π—Π΄Π΅ΡΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ умноТСния</div> 

     function mkChange(item) { var kg = document.getElementById('kg').value; document.getElementById('total').innerHTML = item.value * kg; }; function mkChange1(item) { var select = document.getElementById("mk"); var fruct = select.options[select.selectedIndex].value; document.getElementById('total').innerHTML = item.value * fruct; }; 
     <form> <select id="mk" onchange="mkChange(this);"> <option value="10">Π‘Π°Π½Π°Π½ - 10p</option> <option value="20">Π―Π±Π»ΠΎΠΊΠΎ - 20p</option> <option value="30">Π“Ρ€ΡƒΡˆΠ° - 30p</option> <option value="40">Π’ΠΈΠ½ΠΎΠ³Ρ€Π°Π΄ - 40p</option> </select> <input type="number" id="kg" value="0" min="1" max="50" step="1" placeholder="Π£ΠΊΠ°ΠΆΠΈΡ‚Π΅ ΠΏΡ€Π΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ количСство (ΠΊΠ³)" onkeyup="mkChange1(this);" onchange="mkChange1(this);"> </form> <div>Π—Π΄Π΅ΡΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ умноТСния: --- <span id="total">00</span>p</div> 

    • and then what is necessary ATP! - Aiba

     calculate = function() { var e = document.getElementsByTagName("select")[0]; var price = e.options[e.selectedIndex].attributes['data-price'].value; var amount = document.getElementsByTagName("input")[0].value; var fullprice = parseInt(price) * parseInt(amount); document.getElementById("results").innerText = fullprice; } 
     <form> <select onchange=calculate()> <option value="Banan" data-price="2">Π‘Π°Π½Π°Π½</option> <option value="Apple" data-price="3">Π―Π±Π»ΠΎΠΊΠΎ</option> <option value="Pear" data-price="4">Π“Ρ€ΡƒΡˆΠ°</option> <option value="Grapes" data-price="5">Π’ΠΈΠ½ΠΎΠ³Ρ€Π°Π΄</option> </select> <input onchange=calculate() type="number" value="amount" min="1" max="50" step="1" placeholder="Π£ΠΊΠ°ΠΆΠΈΡ‚Π΅ ΠΏΡ€Π΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ количСство (ΠΊΠ³)"> </form> <div id="results">Π—Π΄Π΅ΡΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ умноТСния</div> 

    • what you need ATP! - Aiba