form in javascript

To form in JavaScript, you can use HTML forms and use JavaScript to manipulate the form data. Here's an example of a simple form with a button to submit the data:

<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">

  <label for="email">Email:</label>
  <input type="email" id="email" name="email">

  <button onclick="submitForm()">Submit</button>
</form>

<script>
  function submitForm() {
    const nameInput = document.getElementById('name');
    const emailInput = document.getElementById('email');

    const name = nameInput.value;
    const email = emailInput.value;

    // Do something with the form data, like send it to a server or log it
    console.log(`Name: ${name}, Email: ${email}`);

    // Optional: Clear the form after submitting
    nameInput.value = '';
    emailInput.value = '';
  }
</script>
690 chars
27 lines

In this example, we use getElementById to get the form inputs and value to get their values. We then do something with the form data in the submitForm function, which is called when the user clicks the Submit button. Finally, we optionally clear the form inputs after submitting.

gistlibby LogSnag