0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

消費税計算~JS側~

Posted at

今回は、JS側での消費税計算をする関数を作り、それを表に出力できるようにしました。

HTML側

index.html
  <table class="tax-answer" id="tax-answer" border="1">
    <caption>結果一覧</caption>
    <tr>
      <th>何月何日</th><th></th><th>結果</th>
    </tr>
    <tr>
      <th>データ</th><th>データ</th><th id="total1">データ</th>
    </tr>
    <tr>
      <th>データ</th><th>データ</th><th id="total2">データ</th>
    </tr>
  </table>

今回は、結果の欄に出力できるようにしていきます。

index.js
const tax = 0.1;
    function totalTax(taxNot) {
      const total = taxNot + taxNot * tax;
      return total;
    }

    
    document.getElementById('total1').textContent = totalTax(3000);
    document.getElementById('total2').textContent = totalTax(400);

document.getElementByIdでHTMLのid属性を取得します。そして、textContent( これは開始タグから終了タグまでを書き換えてくれます)にtotalTax(値);で関数を呼び出し、代入します。
これで、データを書き換えることができました。

今回の場合は、ユーザーが計算した値を計算したいので、入力イベント発生から書き換えて行きます!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?