0

I have a field and a button.

On pressing the button the field must be populated with some data and an alert message should be displayed. Why is this simple HTML and JS is not working? Alert message is working but the field is not populated.

                      <input
                        id="sample-field"
                        name="sample-field"
                        type="text"
                      />
                      <br>
                      <br>
                      <button
                        type="button"
                        id="process-data"
                        onclick="
                        document.getElementById('sample-field').innerHTML = 'Hello';
                        alert('Hello');"
                      >
                        Process Data
                      </button>
DNèp
  • 47
  • 6

1 Answers1

1

                      <input
                        id="sample-field"
                        name="sample-field"
                        type="text"
                      />
                      <br>
                      <br>
                      <button
                        type="button"
                        id="process-data"
                        onclick="
                        document.getElementById('sample-field').value = 'Hello';
                        alert('Hello');"
                      >
                        Process Data
                      </button>

Update innerHtml to value

Nrujal Sharma
  • 112
  • 1
  • 6
  • Thank you. But why is innerHTML wrong? – DNèp Jun 16 '23 at 09:15
  • 1
    `innerHTML` property is used to access or modify the HTML content inside an element. However, the input element is a void element in HTML, which means it cannot have any HTML content or child elements. Void elements, like input, img, or br, do not have closing tags and cannot contain other elements. Therefore, trying to access or modify innerHTML of an input element will not work as expected because it doesn't have any HTML content. – Nrujal Sharma Jun 16 '23 at 09:29