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

116
Vistas
update only single element while sharing same class

I have below jquery which shows an image preview from file input.

$(".imgshowinput").change(function () {
    readURL(this);
});


function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('.imgshow').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

The above code is working well, but the problem is it updates all img elements using the same class

screenshot screenshot

blade

<div class="card" style="width: 18rem;">
    <img id="studphoto" name="studphoto" class="card-img-top imgshow" src="" alt="Card image cap">
    <div class="card-body">             
        <input type="file" name="spic" class="imgshowinput" accept="image/*">
    </div>
</div>
<div class="card" style="width: 18rem;">
<img id="aadphoto" name="aadphoto" class="card-img-top imgshow" src="" alt="Card image cap">
    <div class="card-body">
        <input type="file" name="aadpic" class="imgshowinput" accept="image/*">
    </div>
</div>

ANy suggestion to update only specific img element while using same class ?

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

0

Use closet method of jquery as below

$(input).closest('.imgshow'). attr('src', e.target.result);

and complete javascript

$(".imgshowinput").change(function () {
readURL(this);
});
function readURL(input) {
   if (input.files && input.files[0]) {
     var reader = new FileReader();

     reader.onload = function (e) {
          $(input).closest('.imgshow'). attr('src', e.target.result);
     }

    reader.readAsDataURL(input.files[0]);
 }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Just update your readURL function like this:

function readURL(input) {
  // Gets .card for this input
  var card = $(input).parent().parent();
  
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) { 
          // Finds img inside of .card and sets the attribute    
          $(card.find('img').attr('src', e.target.result));
        }
        reader.readAsDataURL(input.files[0]);
    }
}

I solved it for you on my codepen page.

$(".imgshowinput").change(function () {
    readURL(this);
});

function readURL(input) {
  var card = $(input).parent().parent();
  
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {     
          $(card.find('img').attr('src', e.target.result));
        }
        reader.readAsDataURL(input.files[0]);
    }
}
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  color: #fcbe24;
  background-color: #18222d;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.test {
  display: flex;
}
img {
  width: auto;
  height: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card" style="width: 18rem;">
    <img id="studphoto" name="studphoto" class="card-img-top imgshow" src="" alt="Card image cap">
    <div class="card-body">             
        <input type="file" name="spic" class="imgshowinput" accept="image/*">
    </div>
</div>
<div class="card" style="width: 18rem;">
<img id="aadphoto" name="aadphoto" class="card-img-top imgshow" src="" alt="Card image cap">
    <div class="card-body">
        <input type="file" name="aadpic" class="imgshowinput" accept="image/*">
    </div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Just add the id of the img you want to change so the function would be:

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#studphoto.imgshow').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}
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