// Get references to form elements const myTextInput = document.getElementById('myText'); const mySelectInput = document.getElementById('mySelect'); const myCheckboxInput = document.getElementById('myCheckbox'); const submitButton = document.getElementById('submitForm'); const outputDiv = document.getElementById('output'); if (myTextInput && mySelectInput && myCheckboxInput && submitButton && outputDiv) { // Read initial values console.log('Initial Text:', myTextInput.value); console.log('Initial Select:', mySelectInput.value); console.log('Initial Checkbox:', myCheckboxInput.checked); // Update values programmatically myTextInput.value = 'Hello from JS!'; mySelectInput.value = 'option2'; // Set by option value myCheckboxInput.checked = true; // Attach an event listener to the submit button (or form itself) submitButton.addEventListener('click', (event) => { event.preventDefault(); // Prevent default form submission for demonstration const currentText = myTextInput.value; const currentSelect = mySelectInput.value; const currentCheckbox = myCheckboxInput.checked; const results = `Text Input: "${currentText}" ` + `Select Input: "${currentSelect}" ` + `Checkbox Checked: ${currentCheckbox}`; outputDiv.textContent = results; console.log('Current Form Values: ', results); }); console.log('Form interaction example ready.'); } else { console.error('One or more form elements not found.'); } /* HTML structure for testing:
*/