How to remove the image attribute?

Asked

Viewed 93 times

-1

I have a img within a a and I want to remove the image, but I can’t. I’m using jQuery.

HTML:

<a href="https://api.whatsapp.com/send?phone=55" class="whats-footer" target="_blank"><img src="imagens/whats.png" class="img-js" alt=""></a>

JAVASCRIPT:

<script type="text/javascript">
        $(window).scroll(function() {
            if ($(this).scrollTop() > 50) {
                $('a').removeClass('whats-footer');
                $('a').removeAttr('img');
            }
        });
 </script>

3 answers

1

Face to img is not a link attribute, but an element within the link

To remove the image that is on the link you can simply do $('a > img').remove();

<a class="whats-footer" target="_blank">
<img src="https://unsplash.it/200/200" class="img-js" alt="">
</a>

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
    $(window).scroll(function() {
        if ($(this).scrollTop() > 50) {
            $('a').removeClass('whats-footer');
            $('a > img').remove();
        }
    });
</script>

  • opa, thanks. I started a little while ago in this branch of javascript and started messing with jquery too... so I don’t know much about this kkk

  • @Felipemoreira No problem my dear, an important tip that I will give you is to study soon about the selectors, to understand 'a > img' works and how it works. Not much are the selectors, and you will need it straight. so study soon this part that will help you in everything!

0

You can use the .remove(); in this way:

$(".img-js").remove();

Jquery Remove() and Empty()

In the example I used the classe from the image to the selection, since it did not have a ID, but it is recommended to use the ID, because if you have several images of that class, they will all be removed.

  • Mmm, I usually use classes msm kkkk... rarely use ids. your method also worked and helped a lot, vlw.

  • Consider starting to use Ids more often, for the reason I mentioned in the answer ;). I’m glad the answer helped you, I would appreciate it if you gave a up in my reply. Thank you!

-1

You don’t have access to HTML ? why not just delete img ?

Anyway, you can take it with CSS

a[href='https://api.whatsapp.com/send?phone=55'] img {
     display:none;
}
  • I imagine he wants to take the picture in running time by script, do not completely remove the image from the code.

  • I understand, so I don’t have a solution

Browser other questions tagged

You are not signed in. Login or sign up in order to post.