Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
JavaScript Arrow function not working as expected
const showLocation = () => {
    const locationName = document.getElementById('locationName')
    const selectedElement = document.querySelector('input[name="selected_location"]:checked').value
    locationName.innerHTML = selectedElement
}

The HTML

          <div class="bottom" onchange="showLocation()">
            <ul>
              <li>
                <input type="radio" id="selected_location_nyr" name="selected_location" value="NEW_YORK_ROOT">
                <label for="selected_location_nyr">New York Root</label>
              </li>

The error I get is

(index):120

   Uncaught ReferenceError: showLocation is not defined
at HTMLDivElement.onchange ((index):120:61)

The JavaScript declaration

<script src="javascript/test-123.js" defer></script>
<title>Document</title>
</head>

It's in my head tag but I added defer to it so that the page will continue to load.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on your comments I edit the answer, I guess what you need is something like this: Click on the Run Snippet to see the result

const radioInput = document.querySelector("#selected_location_nyr");
const locationName = document.querySelector("#selected_location");

radioInput.addEventListener("change", showLocation);

function showLocation() {
  locationName.value = radioInput.value;
}
<div class="bottom">
      <ul>
        <li>
          <label for="selected_location_nyr">New York Root</label>
          <input
            type="radio"
            id="selected_location_nyr"
            name="selected_location"
            value="NEW_YORK_ROOT"
          />
        </li>
        <li>
          <label for="selected_location">Selected Location</label>
          <input type="text" name="selected_location" id="selected_location" />
        </li>
      </ul>
    </div>

The section below shows how you can achieve the above functionality for multiple radio inputs

const radioInputs = document.querySelectorAll('input[type="radio"]');

radioInputs.forEach((radio) => {
  radio.addEventListener("change", showLocation);
});

const locationName = document.querySelector("#selected_location");

function showLocation() {
  locationName.value = this.value;
}
    <div><input type="radio" name="select_location" value="1" /> 1</div>
    <div><input type="radio" name="select_location" value="2" /> 2</div>
    <div><input type="radio" name="select_location" value="3" /> 3</div>
    <div><input type="radio" name="select_location" value="4" /> 4</div>
    <div class="bottom">
      <ul>
        <li>
          <label for="selected_location">Selected Location</label>
          <input type="text" name="selected_location" id="selected_location" />
        </li>
      </ul>
    </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda