0

I Need to filter out all images with src domain like abs.twimg.com

<img class="ProfileCard-avatarImage js-action-profile-avatar" src="https://abs.twimg.com/..." alt="">

What's the easiest way to achieve this in Javascript?

2
  • 2
    $('img').filter(function(){ this.src.indexOf('abs.twimg.com')>-1; }) Commented Jun 6, 2016 at 9:52
  • @Rayon The perfect solution! Commented Jun 6, 2016 at 10:10

2 Answers 2

1

try this

var selectedImages = document.querySelectorAll( "img[src*='abs.twimg.com']" );

check this doc

E[foo*="bar"] - an E element whose foo attribute value contains the substring bar

If you want to select images other than those containing abs.twimg.com, then try using a not selector

var selectedImages = document.querySelectorAll( "img:not([src*='abs.twimg.com'])" );
Sign up to request clarification or add additional context in comments.

Comments

0

if by "filter out" you mean remove, then you can do the following:

var nodes = document.getElementsByTagName("img"); 
// or document.querySelectorAll if you don't care too much for compability
for(var i=0;i<nodes.length;i++){
    if(nodes[i].src.indexOf("abs.twimg.com") !== -1){
        nodes[i].parentElement.removeChild(nodes[i]); // remove
    }
}

1 Comment

Hi, for "fiter out" I meant skip those img with that domain in src

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.