Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="wrap">

	<h1>料金シミュレーター</h1>

	<table class="table">
		<thead>
			<th>本体価格</th>
			<th>オプション 1</th>
			<th>オプション 2</th>
		</thead>
		<tbody>
			<td><input type="text" id="js-input-price" value="3000"></td>
			<td>
				<label>
					<input type="checkbox" id="js-check-option-1" checked>
					<span>+1,000円</span>
				</label>
			</td>
			<td>
				<label>
					<input type="checkbox" id="js-check-option-2">
					<span>ー2,000円</span>
				</label>
			</td>
		</tbody>
	</table>

	<div>
		<p>合計(税抜)</p>
		<input type="text" readonly id="js-input-total-price">
	</div>

	<div>
		<p>合計(税込)</p>
		<input type="text" readonly id="js-input-total-price-in-tax">
	</div>
	
</div>

<hr>

<div class="wrap">
	<p>条件</p>
	<ul class="collection">
		<li class="collection-item">本体価格入力時、数値以外を入力したらエラーメッセージを入力欄の下に出す</li>
		<li class="collection-item">本体価格入力時、合計金額を表示する(税込、税抜)<br>エラーメッセージは非表示にする</li>
		<li class="collection-item">オプション1, 2のチェックが変わったら、合計金額を表示する(税込、税抜)</li>
		<li class="collection-item">オプション1がチェックなら、金額を+1,000円</li>
		<li class="collection-item">オプション2がチェックなら、金額をー2,000円</li>
		<li class="collection-item">合計(税込み)は、「税抜の金額 * 1.1」 で計算する</li>
		<li class="collection-item">ページ読み込み時に、本体価格が入力されていたりチェックボックスがチェック済みなら、合計金額を表示する(税込、税抜)</li>
	</ul>
</div>
              
            
!

CSS

              
                .wrap {
	padding: 40px;
	width: 800px;
}
.wrap > * {
	margin-bottom: 2em;
}

h1 {
	font-size: 2em;
	font-weight: bold;
}

input[type=text]:not(.browser-default)[readonly] {
	border: none;
	background-color: #eee;
	margin-top: 8px;
}
              
            
!

JS

              
                /**
  * 必要な要素を最初に全部取得しておく
  */
const $inputPrice = $('#js-input-price');
const $checkOption1 = $('#js-check-option-1');
const $checkOption2 = $('#js-check-option-2');
const $inputTotalPrice = $('#js-input-total-price');
const $inputTotalPriceInTax = $('#js-input-total-price-in-tax');

/**
  * 合計金額計算の関数
  */
const calc = function() {
	// 入力した値を取得して数値に変換(入力した値が無いなら0とする)
  const price = Number($inputPrice.val()) ?? 0;
	// チェック済かどうかを取得
  const isCheckedOption1 = $checkOption1.prop('checked');
  const isCheckedOption2 = $checkOption2.prop('checked');
	// 合計金額を取得する関数
	// (数値で返ってくる)
  const getTotalPrice = function() {
    let resultPrice = price;
    if(isCheckedOption1) {
      resultPrice = resultPrice + 1000;
    }
    if(isCheckedOption2) {
      resultPrice = resultPrice - 2000;
    }
    return resultPrice;
  }
	// 合計のinputに合計金額を反映
	// Math.floorで小数点以下切り捨て、toLocaleStringでカンマ付ける処理を行う
  $inputTotalPrice.val(getTotalPrice().toLocaleString());
  $inputTotalPriceInTax.val(Math.floor(getTotalPrice() * 1.1).toLocaleString());
}

/**
  * エラーメッセージ要素を生成する関数
  */
const createErrorMessage = function() {
  $inputPrice.after('<div id="js-create-error-message" style="display: none;">Error!</div>');
}

/**
  * エラーチェック(本体価格に数値以外を入力したらエラーメッセージ表示)の関数
  * (エラーならtrue、エラー無しならfalse を返す)
  */
const errorCheck = function() {
	// 入力した値を取得
  const value = $inputPrice.val();
	// 数値かどうかをチェックする(正規表現)
	// 数値じゃないならエラーメッセージを表示する
  if(!value.match(/^[0-9]*$/)) {
    $('#js-create-error-message').show();
    return true;
  } else{
    $('#js-create-error-message').hide();
    return false;
  }
}

// ページ全体が読み込まれたら実行
$(window).on('load', function() {
  createErrorMessage();
	const isError = errorCheck();
  if(!isError) {
    calc();
  }
});

// 本体価格入力時に実行
$inputPrice.on('input', function() {
  const isError = errorCheck();
  if(!isError) {
    calc();
  }
});

// オプションチェック変更時に実行
$checkOption1.on('input', function() {
  const isError = errorCheck();
  if(!isError) {
    calc();
  }
});
$checkOption2.on('input', function() {
  const isError = errorCheck();
  if(!isError) {
    calc();
  }
});

              
            
!
999px

Console