Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
¿Cómo ejecutar una función de javascript al seleccionar una selección específica?

Estoy pensando en ejecutar una función de javascript al seleccionar una selección específica, como:

 <select name="fruit"> <option value="1">orange</option> <option value="2">apple</option> <option value="3">banana</option> </select>

Y si selecciono "naranja", aparecerá una imagen naranja como:

 function department() { let form = document.basic; let choice = form.fruit.value; if (choice === 1) { document.getElementById("orange_img").style.display = "inherit"; } }

¿Cómo puedo hacer que este javascript funcione?

PD: ¿Hay métodos sin jquery?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede agregar eventListener al evento onchange , verificar el valor y agregar lógica personalizada para manejarlo. ¡No hay necesidad de jquery!

 const fruitSelect = document.getElementById('fruit'); fruitSelect.addEventListener('change', (e) => { const value = e.target.value; if(value === '1') { // corresponds to orange // do stuff here } });
 <select name="fruit" id="fruit"> <option value="1">orange</option> <option value="2">apple</option> <option value="3">banana</option> </select>

about 4 years ago · Juan Pablo Isaza Report

0

Almacene los enlaces a las imágenes en un objeto, y luego, cuando su opción cambie, actualice el elemento con él.

 // List of images const images = { orange: 'https://dummyimage.com/100x100/ffa600/000.png', apple: 'https://dummyimage.com/100x100/2e8b56/000.png', banana: 'https://dummyimage.com/100x100/FFFF00/000.png' }; // Cache the elements const select = document.querySelector('select'); const div = document.querySelector('div'); // Add an event listener to the select select.addEventListener('change', handleChange, false); // Update the background image of the div // with the new image function handleChange() { const img = `url(${images[this.value]})`; div.style.backgroundImage = img; }
 div { height: 100px; width: 100px; }
 <select> <option disabled selected>Choose a colour</option> <option value="orange">orange</option> <option value="apple">apple</option> <option value="banana">banana</option> </select> <div></div>

about 4 years ago · Juan Pablo Isaza Report

0

simplemente agregue un evento onChange y dentro de onChange puede invocar cualquier función js. por ejemplo: <select onchange="myFunction()">

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!