Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

366
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda