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

              
                <main class="container mt-5 mb-5" lang="ja">

<h1 class="mb-3">設定</h1>

<div class="bg-primary-subtle border border-light-subtle" id="messagebox" aria-live="polite">
<p class="mb-0 p-3" id="message"></p>
</div>

<form class="mt-5" id="myForm" >

<label class="form-label fs-5" for="inputField">設定項目1</label>
<input type="text" class="form-control border-dark rounded-0" id="inputField">

<fieldset class="mt-4">
<legend class="fs-5">設定項目2</legend>
<div class="form-check">
<input type="checkbox" class="form-check-input border-dark" id="checkBox1">
<label class="form-check-label" for="checkBox1">機能 A を有効にする</label>
</div>
<div class="form-check"> 
<input type="checkbox" class="form-check-input border-dark" id="checkBox2">
<label class="form-check-label" for="checkBox2">機能 B を有効にする</label>
</div>
</fieldset>

<fieldset class="mt-4">
<legend class="fs-5">設定項目3</legend>
<div class="form-check">
<input type="radio" class="form-check-input border-dark" id="radiobutton1" name="radioGroup" value="option1">
<label class="form-check-label" for="radiobutton1">オプションその1</label>
</div>
<div class="form-check">
<input type="radio" class="form-check-input border-dark" id="radiobutton2" name="radioGroup" value="option2">
<label class="form-check-label" for="radiobutton2">オプションその2</label>
</div>
</fieldset>

<button type="submit" class="btn btn-primary rounded-0 mt-5" id="submitButton">保存</button>

</form>
  
</main>
              
            
!

CSS

              
                /* JS が有効の場合にのみ、(サブミットボタンの無効化に伴って) メッセージボックスを表示させたいので、いったん display:none; にする。 */
#messagebox{display:none;}
              
            
!

JS

              
                const form = document.getElementById("myForm");
const formControls = form.querySelectorAll("input, textarea, select");
const submitButton = document.getElementById("submitButton");
const message = document.getElementById("message");
const messagebox = document.getElementById("messagebox");

// メッセージボックスのデフォルト設定
messagebox.style.display ="block";
message.textContent = "設定を変更すると、ページ末尾の「保存」ボタンが有効になります。";

// サブミットボタンをデフォルトで disabled にする。
submitButton.setAttribute("disabled", "disabled");

// 各フォーム入力要素の初期設定
document.getElementById("inputField").value = "hogehoge" ;
document.getElementById("checkBox1").checked = "true" ;
document.getElementById("checkBox2").checked = "true" ;
document.getElementById("radiobutton1").checked = "true" ;

// コントロールの変更を監視
formControls.forEach(control => {
  control.addEventListener("input", updateSubmitButtonState);
  control.addEventListener("change", updateSubmitButtonState);
});

// いずれかのコントロールの値が変更された場合、submitButtonを有効化
function updateSubmitButtonState() {
  let anyValueChanged = false;
  formControls.forEach(control => {
    if (control.type !== "radio" || control.checked) {
      if (control.type === "checkbox" || control.type === "radio") {
        if (control.checked !== control.defaultChecked) {
          anyValueChanged = true;
        }
      } else if (control.value !== control.defaultValue) {
        anyValueChanged = true;
      }
    }
  });

  if (anyValueChanged) {
    submitButton.removeAttribute("disabled"); //サブミットボタンを有効化
    message.textContent = "設定が変更されました。設定内容を保存するには、ページ末尾の「保存」ボタンを押してください。";
    messagebox.classList.remove("bg-primary-subtle");
    messagebox.classList.add("bg-danger-subtle"); //メッセージボックスの背景色を差し替え
  }
}


// フォームの送信時のダミー処理
form.addEventListener("submit", function(event) {
  event.preventDefault(); // フォームの実際の送信を防ぐ
  submitButton.setAttribute("disabled", "disabled"); //サブミットボタンを無効化
  message.textContent = "設定が保存されました。";
  messagebox.classList.remove("bg-danger-subtle");
  messagebox.classList.add("bg-success-subtle"); //メッセージボックスの背景色を差し替え
});
              
            
!
999px

Console