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

442
Vistas
Why does this onchange function not work with option:selected?

This has to be simple: why does this on change function not work with option:selected?

And, why does my variable state not "reload" on change of the option?

var states = ["AL", "AK", "AZ", "VT"];

$('#shipping_state').on('change', function() {

  var state = $("#shipping_state option:selected").text();
  console.log(state);

  jQuery("#shipping_state_field").append("<div id=\"no-ship-state\">Sorry, due to " + state + " state law, alcohol can't be shipped to a " + state + " address</div>");

  if (states.includes($(this).val())) {
    jQuery("#no-ship-state").show();
  } else {
    jQuery("#no-ship-state").hide();
  }
});
#no-ship-state {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<select name="shipping_state" id="shipping_state" data-label="State / County">
  <option value="">Select an option&hellip;</option>
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
  <option value="VT">Vermont</option>
</select>

<div id="shipping_state_field"></div>

<div id="no-ship-state"></div>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You're appending the "shipping_state_field" over and over again. I'd recommend instead using a div in your markup that you edit whenever necessary instead of adding a new one every time you want the text to appear. The example below tries to stay as close as possible to your original code:

var states = ["AL", "IL", "MI", "MS", "UT", "VT", "NH"];

$('#shipping_state').on('change', function() {

  var state = jQuery("#shipping_state option:selected").text();

  jQuery("#no-ship-state").html("Sorry, due to " + state + " state law, alcohol can't be shipped to a " + state + " address");

  if (states.includes($(this).val())) {
    jQuery("#no-ship-state").show();
  } else {
    jQuery("#no-ship-state").hide();
  }
});
#no-ship-state {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<select name="shipping_state" id="shipping_state" data-label="State / County">
  <option value="">Select an option&hellip;</option>
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
</select>

<div id="shipping_state_field"></div>

<div id="no-ship-state"></div>

This way .html() is used to replace the content of the div instead of appending an new one every time. Here's some references about the details:

https://developer.mozilla.org/en-US/docs/Web/API/Element/append

https://api.jquery.com/html/

about 4 years ago · Juan Pablo Isaza Denunciar

0

As mentioned in comments, you should't append the DIV each time the user selects from the dropdown, as this creates duplicate IDs.

Create the DIV statically, and just update the name of the state in it. Then hide or show it.

var states = ["AL", "AK", "AZ", "VT"];

$('#shipping_state').on('change', function() {

  var state = $("#shipping_state option:selected").text();
  console.log(state);

  if (states.includes($(this).val())) {
    $(".state_name").text(state);
    jQuery("#no-ship-state").show();
  } else {
    jQuery("#no-ship-state").hide();
  }
});
#no-ship-state {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<select name="shipping_state" id="shipping_state" data-label="State / County">
  <option value="">Select an option&hellip;</option>
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
  <option value="VT">Vermont</option>
  <option value="NY">New York</option>
</select>

<div id="no-ship-state">Sorry, due to <span class="state_name"></span> state law, alcohol can't be shipped to a <span class="state_name"></span> address</div>

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