Skip to content
Advertisement

Edit The Src Of a Image

HTML Code

<img class="blog-picture ul-normal-classic" src="https://example.pk/wp-content/uploads/2020/12/example300-300x203.jpg" alt="Beintehaa300">

Whta I Need:

<img class="blog-picture ul-normal-classic" src="https://example.pk/wp-content/uploads/2020/12/example300.jpg" alt="Beintehaa300">

I just want to remove (-300×203) from the src. Is this possible using js or PHP or any other language I want to implement this code in WordPress?

Advertisement

Answer

<script>
document.addEventListener("DOMContentLoaded", function(event) {
    var allImages = document.querySelectorAll('img');
    for (var i = 0; i < allImages.length; i++) {
        var imageSource = allImages[i].getAttribute('src');

        if (imageSource.includes('-300x203')) {
            var replacedSource = allImages[i].getAttribute('src').replace('-300x203', '');
            allImages[i].setAttribute('src', replacedSource);
        }
    }
});
</script>

Put this script on page

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement