Javascript: Change background and show selections in TEXTAREA resulting in error 405

Asked

Viewed 24 times

0

Hello! This is the following exercise:

Click on the go button:

  • Change the background color
  • Show selection of newspapers in the textarea

When I click on the button, the page results in error 405. I am not finding the bug and neither in the console nor in VS is appearing error.

The HTML is like this:

<body>
  <h1>Radio buttons and Checkboxes</h1>
  <form action="" method="post" id="backg">
    <div>Please select a background color for the form:</div>
    <div>
      <label><input type="radio" name="colors" value="green" />Green </label>
      <label
        ><input type="radio" name="colors" value="yellow" />Yellow
      </label>
      <label><input type="radio" name="colors" value="lime" />Lime </label>
      <label><input type="radio" name="colors" value="aqua" />Aqua </label>
    </div>
    <div>
      Please select your favorite magazines: <br />
      <label>
        <input type="checkbox" name="mags" value="The New Yorker" />
        The New Yorker </label
      ><br />
      <label>
        <input type="checkbox" name="mags" value="WIRED" />
        WIRED </label
      ><br />
      <label>
        <input type="checkbox" name="mags" value="National Geographic" />
        National Geographic </label
      ><br />
      <label>
        <input type="checkbox" name="mags" value="Sports Illustrated" />
        Sports Illustrated
      </label>
    </div>
    <div>
      <label>
        Your magazine selections:<br />
        <textarea name="result" cols="30" rows="4" readonly></textarea>
      </label>
    </div>
    <div>
      <input type="submit" name="button" value="Select" id="selectBtn" />
      <input type="reset" name="Reset" value="Clear" id="resetBtn" />
    </div>
  </form>
</body>

Javascript looks like this:

<script>
  function select() {
    const myForm = document.getElementById('backg');
    const colors = myForm['colors'];

    for (let i = 0; i < colors.length; i++) {
      if (colors[i].checked) {
        let userChoice = colors[i].value;
        document.querySelector('body').style.backgroundColor = userChoice;
      }
    }
  }

  function displayMags() {
    const myFormTwo = document.getElementById('backg');
    const magazine = selectForm['mags'];

    let message = magazine;
    myFormTwo['result'].value = message;
  }

  document.getElementById('selectBtn').onclick = select;
  document.getElementById('selectBtn').onclick = displayMags;
</script>
  • That doesn’t happen because of this input of the kind submit? This way Oce is submitting the form to an address that the server cannot solve (action=""). You shouldn’t put the type button for this input? Would look like this: <input type="button" name="button" value="Select" id="selectBtn" />

  • Something else, when Voce assigns document.getElementById('selectBtn').onclick = displayMags;, You may be overwriting the previous assignment document.getElementById('selectBtn').onclick = select;.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.