Skip to content
Advertisement

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 ?

Advertisement

Answer

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>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement