-1

it's me again. How I can call data from defined array?

var myArray = new Array("http://www.gravatar.com/avatar.php", "http://1.gravatar.com/avatar/", "http://0.gravatar.com/avatar/");

$('img[src^="DATA FROM myArray"]').remove()

6 Answers 6

2
$(myArray).each(function(idx,elem){
    /*idx is item index, elem is the item itself*/
    $('img[src^="'+elem+'"]').remove();
})
Sign up to request clarification or add additional context in comments.

4 Comments

I believe this solution is the simpliest and the best one.
@Mister X: Don't forget to use the cleaner array literal notation in my answer.
Ow.. i'm not a jquery coder, just PHP. But what you mean with cleaner arrays?
@Mister X: var array = [1, 2, 3] instead of var array = new Array(1, 2, 3)
2

If you want to select all that elements, independent from what you want to do with them, you could do:

var $elements = $();

for(var i = myArray.length;i--;) {
    $elements.add($('img[src^="' + myArray[i] + '"]'));
}

You should use array literals [...] instead of the array constructor.

Comments

1
var myArray = [
    "http://www.gravatar.com/avatar.php",
    "http://1.gravatar.com/avatar/",
     "http://0.gravatar.com/avatar/"
];

$('img').filter(function() {
    var inArray = false;
    var src = $(this).attr('src');
    $.each(myArray, function() {
        if(src.indexOf(this) == 0)
            inArray = true;
    }
    return inArray;
}).remove()

Or you could just use regex:

$('img').filter(function() {
    return $(this).attr('src')
                  .match(/^http:\/\/(www|0|1)\.gravatar\.com\/avatar(\.php)?/i);
}).remove()

Comments

1

You can do something like this:

$('img[src^="' + myArray[1] + '"]').remove();

Comments

1

Maybe it's faster to fetch like this:

$("img[src]").filter(function() {
  return $.inArray($(this).attr("src"), myArray) != -1;
}).remove();

1 Comment

That doesn't do what was asked. The test is whether the src starts with an array element, not is an array element.
0

InArray let's you get an the index of an item within an array. So:

myArray[$.inArray("http://www.gravatar.com/avatar.php",myArray)]

Comments

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.