'find if a img have alt in jquery if not then add from array
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.
Solution 1:[1]
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.
});
Solution 2:[2]
This should help get you started:
$("#body img").each(function(){
var $this = $(this);
if (!$this.attr("alt")){
//do something
}
});
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gdoron is supporting Monica |
| Solution 2 | |
| Solution 3 | RagulDevaraj |
