JAVASCRIPT

Get and Set Form Input Values and Handle Change Events

Master how to programmatically retrieve and update values of various form inputs (text, checkbox, select) and efficiently handle their `change` events in JavaScript.

document.addEventListener('DOMContentLoaded', () => {
  const usernameInput = document.getElementById('username');
  const newsletterCheckbox = document.getElementById('newsletter');
  const countrySelect = document.getElementById('country');
  const displayValuesButton = document.getElementById('displayValues');
  const outputDiv = document.getElementById('output');

  // Set initial values programmatically
  if (usernameInput) usernameInput.value = 'GuestUser';
  if (newsletterCheckbox) newsletterCheckbox.checked = true;
  if (countrySelect) countrySelect.value = 'CA'; // Set to Canada

  // Event listener for input change (text input)
  if (usernameInput) {
    usernameInput.addEventListener('input', (event) => {
      console.log(`Username changed to: ${event.target.value}`);
    });
  }

  // Event listener for checkbox change
  if (newsletterCheckbox) {
    newsletterCheckbox.addEventListener('change', (event) => {
      console.log(`Newsletter subscribed: ${event.target.checked}`);
    });
  }

  // Event listener for select change
  if (countrySelect) {
    countrySelect.addEventListener('change', (event) => {
      console.log(`Selected country: ${event.target.value}`);
    });
  }

  // Display current values on button click
  if (displayValuesButton) {
    displayValuesButton.addEventListener('click', () => {
      const username = usernameInput ? usernameInput.value : '';
      const newsletter = newsletterCheckbox ? newsletterCheckbox.checked : false;
      const country = countrySelect ? countrySelect.value : '';

      outputDiv.innerHTML = `
        <p>Username: <strong>${username}</strong></p>
        <p>Newsletter: <strong>${newsletter}</strong></p>
        <p>Country: <strong>${country}</strong></p>
      `;
    });
  }
});

// HTML structure for this example:
// <div id="myFormContainer">
//   <label for="username">Username:</label>
//   <input type="text" id="username" name="username"><br><br>

//   <label for="newsletter">Subscribe to Newsletter:</label>
//   <input type="checkbox" id="newsletter" name="newsletter"><br><br>

//   <label for="country">Country:</label>
//   <select id="country" name="country">
//     <option value="US">United States</option>
//     <option value="CA">Canada</option>
//     <option value="UK">United Kingdom</option>
//   </select><br><br>

//   <button id="displayValues">Display Current Values</button>

//   <div id="output"></div>
// </div>
How it works: This snippet covers the essential aspects of interacting with HTML form inputs using JavaScript. It demonstrates how to retrieve and set the `value` property for text inputs and select elements, and the `checked` property for checkboxes. Crucially, it shows how to attach event listeners (`input` for continuous text updates, `change` for select/checkbox) to react to user interactions, making forms dynamic and responsive.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs