0

first need to find all img in the sites

$("#body").find(img)

and then check if the img have the "alt" attribute, if image have the attribute it'll be escaped and if it not have one or the alt is empty,a string will be randomly added to img from a list or array.

I now that i have to use .each() some how in a function but I'm new to jquery so a little bit help would be perfect.

E P
  • 389
  • 4
  • 16

4 Answers4

6
var arr = ['hello', 'hi', 'yo'];
$('#body img').each(function() {
    if ( ! $(this).attr('alt'))
        $(this).attr('alt', arr[Math.round(Math.random() * (arr.length - 1)))]);
});
Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
  • will this include if alt is a empty string? – E P May 24 '12 at 11:12
  • @EhsanPourhadi: Yes it will, this will give you all images that have either no alt or an empty alt, but will skip all images that already have an alt string – musefan May 24 '12 at 11:58
2

You can do it all with one selector:

$("#body img[alt!='']").each(function(){
    // What you want here...
});

Or this:

$("#body img[alt!=''][alt]").each(function(){

depends on the DOM structure.

Or with a filter function:

$("#body img").filter(function(){
    return this.alt;
}).each(function(){
    // What you want here...
});

If you want to do it all with one each, you can use this:

$("#body img").each(function(){
    if (this.alt)
        // Do something.
    else
        // Do something else.
});
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • also http://stackoverflow.com/questions/7706429/fastest-way-to-find-the-body-tag-via-jquery shows that $('body') is significantly faster than $('#body') to get the body object. Similarly, $('img') is 12% faster than $('body').find('img') – Joe Bowman May 24 '12 at 10:46
  • 1
    @JoeBowman He's not trying to find the `body` object, hes trying to find the `#body` element.. – Curtis May 24 '12 at 10:46
  • @Curt and gdoron: first line of OP states: "first need to find all img in the sites" – Joe Bowman May 24 '12 at 10:47
  • 2
    Curt is right, it should be `alt==''` (he wants all images that do not have an alt value) – musefan May 24 '12 at 10:49
  • @Curt. On which line?, give the whole code section you refer to, – gdoron May 24 '12 at 10:50
  • @musefan. I think he needs the two cases, doesn't he? – gdoron May 24 '12 at 10:50
  • 1
    @gdoron: My assumption is that when he says "it'll be escaped" he means that the loop will skip to the next item (i.e. `continue;`) so he therefore doesn't care for any img that has an alt value – musefan May 24 '12 at 10:52
  • @musefan. Well, It's up to the OP, he can read your comments, and choose what he wants. Thanks for pointing it out! – gdoron May 24 '12 at 10:55
  • tnx a lot, yes i need both cases cuz some image have the alt but it's empty – E P May 24 '12 at 11:27
  • @gdoron: Yes I was right, OP wants images that have either no alt or an empty string alt. He doesn't care about the images that already have an alt. using `alt==''` will match this requirement – musefan May 24 '12 at 11:59
1

This should help get you started:

$("#body img").each(function(){
   var $this = $(this);
   if (!$this.attr("alt")){
     //do something
   }
});
Curtis
  • 101,612
  • 66
  • 270
  • 352
  • Please, use `this.alt` instead of `$(this).attr('alt')` – gdoron May 24 '12 at 10:49
  • @gdoron I'm assuming the OP will be making use of `$this` again, so it would be better to store this as a local variable, would it not? – Curtis May 24 '12 at 10:49
  • 1
    No, because `$this.attr('alt)` is a lot slower than `this.alt` and a lot verbose. – gdoron May 24 '12 at 10:51
  • And actually `$(this)` isn't an expensive operation as you may think. [You can read more here](http://stackoverflow.com/q/10433014/601179) – gdoron May 24 '12 at 10:53
0
Object.keys($('body').find('img')).map((item)=>{
    return $('body').find('img')[item].alt
})

By This method you can get all the image alt which are empty and also you ill get image tag which all are don't have alt attributes.