Javascript

販売手数料、販売利益の出し方

1. windowがloadされたときにイベント発火

2. const ____ = document.getElementById("id")で入力欄と販売手数料、販売利益の要素を取得する

3. 入力されたときにイベント発火するように関数を定義する。

priceInput.addEventListener("keyup", () => {}); で非同期通信を実行

4.入力された金額を定義

5.条件式if で価格を入力したとき、販売手数料と販売利益が反映されるようにする

function price() {
const priceInput = document.getElementById("item-price"); //金額入力
const add_tax = document.getElementById("add-tax-price"); //販売手数料
const profit = document.getElementById("profit"); //販売利益
 
priceInput.addEventListener("keyup", () => {
const value = priceInput.value; //入力の金額定義
 
if (value >= 300 && value <= 9999999) { //300~9999999円の場合の条件分岐
let fee = value * 0.1
let gains = value - fee
add_tax.textContent = fee;
profit.textContent = gains;
} else {
let fee = ""
let gains = ""
add_tax.textContent = fee;
profit.textContent = gains;
}
});
}
window.addEventListener('load', price);