How to Convert Form Data to JSON with HTML Forms | FormsJS

FormsJS is a simple-to-use JavaScript library that covers type subject values to JSON in real-time. The items containing the data category will be analyzed automatically. It will use the Name attribute as the key in the returned JSON object.

How it works

Elements with the data class will automatically be parsed. It will use the name attribute as a key in the returned JSON object.

For checkboxes:

    It will return as true or false.

For radio buttons:

    It will use the value attribute.

For dropdowns:

    It will value of the selected <option>, if none is present the text value will be used.

Must Read: JQuery Library for Linking HTML Forms with JSON | jquery.jsForm

Form Data to JSON with HTML Forms Details

Post NameConvert Form Data To JSON
Author Namee3ndr
CategoryForm Plugins, JSON
Official PageClick Here
Official Websitegithub.com
Publish DateAugust 13, 2020
Last UpdateAugust 8, 2023
DownloadLink Below
LicenseMIT

How to make use of it:

1. Add the CSS class ‘data’ to type fields as follows:

<div id="form">
  <h1 class="title">Form</h1>
  <label class="label">
  Username
  <input class="input data" name="username" type="text" worth="formsjs_" />
  </label>
  <label class="label">
  Send me promotional emails
  <br />
  <input class="checkbox data" name="allow_promotional_emails" type="checkbox" />
  <br />
  </label>
  <label class="label">
    Gender
    <br />
    <div class="select control">
      <select class="data" name="gender">
        <option selected value="unknown">Prefer not to say</option>
        <option value="other">Other</option>
        <option value="female">Female</option>
        <option value="male">Male</option>
      </select>
    </div>
  </label>
  <label class="label">
    Where do you live?
    <br />
    <div class="select control">
      <select class="data" name="zipcode">
        <option value="">Elsewhere</option>
        <option value="94016">San Francisco</option>
        <option value="94088">San Jose</option>
      </select>
    </div>
  </label>
  <div class="control">
    <label class="label">
    Account Type
    </label>
    <label class="radio">
    <input class="data" checked type="radio" name="account_type" value="free" />
    Free Account
    </label>
    <label class="radio">
    <input class="data" type="radio" name="account_type" value="premium" />
    Premium Account
    </label>
  </div>
</div>

2. Convert subject values right into a JSON object:

function submit() {
  let username = document.querySelector("[name=username]");
  let result = FORMSJS.readForm("#form");
  // Make the username box turn red if its length is less than 8
  if (username.value.length < 8) 
      username.classList.remove("is-success");
      username.classList.add("is-danger");
   else 
      username.classList.remove("is-danger");
      username.classList.add("is-success");
  
  console.log(result);
}
Array.from(document.querySelector("#form").getElementsByClassName("data")).forEach((element) => );

3. The JSON object needs to be like these:

{
  "username": "formsjs_",
  "allow_promotional_emails": false,
  "gender": "unknown",
  "zipcode": null,
  "account_type": "free"
}

See Demo And Download

Convert-Form-Data-to-JSON

Official Website(e3ndr): Click Here

This superior jQuery/javascript plugin is developed by e3ndr. For extra Advanced usage, please go to the official website.

Related Posts

HStack-and-VStack-in-CSS

CSS Layout Components Horizontal/Vertical Stack | HStack and VStack

HStack and VStack in CSS – CSS layout components that (basically) stack anything horizontally and vertically. A pure CSS library that makes it easy to stack elements…

Floating-Whatsapp-Chat-Button

How to Add Floating Whatsapp Chat Button In HTML | venom-button

Venom Button is a very simple plugin for the jQuery floating WhatsApp button. Adds a floating button to your site that calls WhatsApp Click to Chat API. It will automatically start the WhatsApp…

Data-Table-Generator-Tabulator

Interactive Data Table Generator with JS/jQuery and JSON | Tabulator

Tabulator allows you to create interactive tables in seconds from any HTML Table, JavaScript array, AJAX data source, or JSON format data. Just include the library in your…

alert-confirm-prompt-attention-js

Simple Alert, Confirm, Prompt Popup Using Vanilla JavaScript Library | attention.js

JavaScript provides various built-in functionality to display popup messages for different purposes. Attention JS is a vanillaJS plugin used to create a custom alert, confirm, or Prompt…

Bootstrap-4-Sidebar-Menu-Responsive-Template

Bootstrap 4 Sidebar Menu Responsive Template | MDB

Bootstrap Side Navbar – Responsive sidebar template based on the Bootstrap 4 framework. An easy-to-use, totally responsive, Google Material Design impressed aspect navigation for modern web app…

Bootstrap-4-Toast-Notification-Plugin

Lightweight Bootstrap 4 Toast Notification Plugin | BS4 Advanced Toast

A lightweight Bootstrap 4 Toast Notification plugin integrated with JS/jQuery. bs4-toast.js is a JavaScript library that enhances the native Bootstrap toast component with icons, buttons, callbacks, and…

Leave a Reply

Your email address will not be published. Required fields are marked *