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

115
Vistas
jQuery: Depending on the Selected Option of the <Select> Output a Different Text

I want a different output based on the selected option.

What I'm trying to do is if the selected option is beyond option 2, I want to enter the condition. If it's not, don't enter the condition.

This is my code:

$(document).ready(function() {
  $('#title').html("Date 1 or 2");

  $('#select-date').change(function() {
    if ($('#select-date') > $('#select-date').is(':nth-child(2)')) {
      $('#title').html("Date 3 to 6");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<div>
  <p id="title"></p>
  <select id="select-date">
    <option>Date 1</option>
    <option>Date 2</option>
    <option>Date 3</option>
    <option>Date 4</option>
    <option>Date 5</option>
    <option>Date 6</option>
  </select>
</div>

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

0

You likely want to look at the selected option

$("#select-date option:selected")

but it is much simpler to use the DOM selectedIndex which is 0-based.

When you use this instead of $(this) you get the underlying DOM object

I am using a so-called ternary to set the text of the title

$(function() {
  $('#select-date')
  .on("change", function() {
    $('#title').html(this.selectedIndex < 2 ? "Date 1 or 2" : "Date 3 to 6");
  })
  .change(); // initialise
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<div>
  <p id="title"></p>
  <select id="select-date">
    <option>Date 1</option>
    <option>Date 2</option>
    <option>Date 3</option>
    <option>Date 4</option>
    <option>Date 5</option>
    <option>Date 6</option>
  </select>
</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