0

I am using swiper for a carousel and I am adding remove functionality to remove images. I want to delete specific div with same name("swiper-slide") using the src of the image inside the div. I don't know if this possible, can anyone help me with this? I am specifically trying to use Javascript and not Jquery.

<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide" id="slide4">
            <img src="/Media/Default/Uploads/test-23.png" border="0" alt="" width="1097" height="361" />
        </div>
        <div class="swiper-slide" id="slide3">
            <img src="/Media/Default/Uploads/knockoutjs-teaser.jpg" border="0" alt="" width="900" height="506" />
        </div>
        <div class="swiper-slide" id="slide1">
            <img src="/Media/Default/Uploads/1556269682222.jpg" border="0" alt="" width="1305" height="548" /> &nbsp;
        </div>
        <div class="swiper-slide" id="slide2">
            <img src="/Media/Default/Uploads/duales-studium-berlin.jpg" border="0" alt="" width="3300" height="2475" /> &nbsp;
        </div>
    </div>
</div>
4
  • Can you please explain an example what you want to achieve? Commented Jul 28, 2021 at 7:56
  • Which div with a specific name are you referring to? None of the elements here have a name attribute Commented Jul 28, 2021 at 7:56
  • @ProfessorAbronsius sorry, my mistake, i meant class name Commented Jul 28, 2021 at 7:58
  • @Nitesh I want to delete a div which contains specific image src Commented Jul 28, 2021 at 7:59

2 Answers 2

2

Try this:

function remove(src) {
    var img = document.querySelector('img[src="' + src + '"]');
    var parent = img.closest('.swiper-slide');
    var grandparent = parent.closest('.swiper-wrapper');
    
    grandparent.removeChild(parent);
}

And pass the src you want to remove.

Demo: https://jsfiddle.net/wvof2qdt/1/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, this along with 'back2dos' answer combined i made it work, thanks for the help
1

You're going to have to find the image and then remove its parent. This should work (not tested):

function removeSlide(imgSrc) {
  var selector = '.swiper-slide>img[src="' + selector + '"]';// selector for the image to be removed
  var image = document.querySelector(selector);// let's get the image
  if (image) {// if an image was found
    image.parentElement.remove();// remove its parent (the div)
  }
  return !!image; // returns true if an image was found
}

1 Comment

Thanks for the answer, this works too. I have tested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.