見出し画像

javascriptで一括税込価格にしたいとき

今まで税抜き表記だった部分を、税込表記に変更したいときのcode備忘録!

 <!-- html -->
<h1>ケーキ屋さん</h1>
<h2>好きなケーキを選んでね</h2>
<table class="c-wrapp">
   <tr>
       <th>ショートケーキ</th>
       <td>500円</td>
   </tr>
   <tr>
       <th>モンブラン</th>
       <td>1000円</td>
   </tr>
   <tr>
       <th>マカロン</th>
       <td>800円</td>
   </tr>
   <tr>
       <th>チョコケーキ</th>
       <td>1200円</td>
   </tr>
   <tr>
       <th>シュークリーム</th>
       <td>200円</td>
   </tr>
</table>​
// js
//head内css直下にjsファイルをよんでいるため"DOMContentLoaded"から

document.addEventListener('DOMContentLoaded', function () {
   const elements = document.querySelectorAll('td')

   for (let i = 0; i < elements.length; i++) {
       const price = elements[i].textContent.slice(0, -1)
       const withTax = Math.round(price * 1.1)
       elements[i].textContent = withTax + '円'
       console.log(price)
   }
   console.log(elements)

})



この記事が気に入ったらサポートをしてみませんか?