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

361
Visualizações
Updating fields based on selection

Been trying to update fields using html select and JavaScript and dropdowns but I can't get it to constantly change Here is the code Been trying to update fields using html select and JavaScript and dropdowns but I can't get it to constantly change Here is the code:

<div class="card1">
<div class="circle"></div>
<div class="content">
    <h2>Fifa 22 <div class="txt1"></div></h2>  
    <div class="select">
        <select name="fifa22" id="editions">
            <option value="Ultimate">Ultimate</option>
            <option value="Standard">Standard</option>
            <option value="Legacy">Legacy</option>
          </select>
          <img src="images/loading.png" id="loadingpng" alt="">
    </div>
    <a href="#" class="btn">Buy Now <div class="txt2"></div></a>                
</div>
<img src="images/fifaPs.jpg" id="img1" alt="">
const txt1 = document.querySelector('.txt1');
const txt2 = document.querySelector('.txt2');

txt1.innerText = 'Ultimate'
txt2.innerText = '$99.99'

var card = document.getElementById("editions");
var selectedValue = card.options[card.selectedIndex].value;

if (selectedValue == "Ultimate") {
    txt1.innerText = 'Ultimate'
    txt2.innerText = '$99.99'
}else if (selectedValue == "Standard") {
    txt1.innerText = 'Standard'
    txt2.innerText = '$59.99'
}else if (selectedValue == "Legacy") {
    txt1.innerText = 'Legacy'
    txt2.innerText = '$79.99'
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your code is not updating as it only runs once. To make it run every time something happens to the select element you need to define and attach an "event handler" - a function that runs in response to an event, like a click. There are two ways to link the event to the function that "handles" it.

  1. In HTML by adding it as an attribute of the select element. HTML Attribute events
    <select onchange="myHandlerFunction">
  1. In JS by using the .addEventListener method on the element. JS dom events
    const card = document.getElementById("editions")
    card.addEventListener("change", "myHandlerFunction")
  • Method 1 is a little more intuitive. It's okay for tiny jobs and to use while you're learning.

  • Method 2 tends to be preferred as it separates concerns and languages.

Handler functions are passed event objects as their first parameters. This object contains a reference to the element that triggered the event e.g.

    function myEventHandler(event){
        const selectedValue = event.target.value
        // blah blah blah
    }

If your projects start to require a lot of event handlers you should look up 'event delegation' TL;DR using one big event handler on an element further up the DOM tree.

about 4 years ago · Juan Pablo Isaza Relatório

0

The JS script is always run first. So your code is actually not responding to any event. You need a function as an event listener for that. A parameter event is always passed to eventListeners and you can use that to get the value you are looking for.

Note also that the attribute selected can be used to select an option by default. So no need to write code for that in your JS. Keep it simple.

<div class="card1">
<div class="circle"></div>
<div class="content">
    <h2>Fifa 22 <div class="txt1"></div></h2>  
    <div class="select">
        <select name="fifa22" id="editions" onchange="selectChange()">
            <option value="Ultimate" selected>Ultimate</option>
            <option value="Standard">Standard</option>
            <option value="Legacy">Legacy</option>
          </select>
          <img src="images/loading.png" id="loadingpng" alt="">
    </div>
    <a href="#" class="btn">Buy Now <div class="txt2"></div></a>                
</div>
<img src="images/fifaPs.jpg" id="img1" alt="">

const txt1 = document.querySelector('.txt1');
const txt2 = document.querySelector('.txt2');

txt1.innerText = 'Ultimate'
txt2.innerText = '$99.99'

var card = document.getElementById("editions");


function selectChange(event) {
var selectedValue = event.target.value;

if (selectedValue == "Ultimate") {
    txt1.innerText = 'Ultimate'
    txt2.innerText = '$99.99'
}else if (selectedValue == "Standard") {
    txt1.innerText = 'Standard'
    txt2.innerText = '$59.99'
}else if (selectedValue == "Legacy") {
    txt1.innerText = 'Legacy'
    txt2.innerText = '$79.99'
}
}

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