JQuery: get every src image

I have a series of images, each with a "photo" class;

I want to go through each of them and restore the source of the photo so that I can use it later in the if statement. To do this, I wrote the code below, but was not successful:

$.each($(".photo"), function() {
  var imgsrc = $(this).attr("src").length;
  console.log(imgsrc);
});

I'm not sure where I made a mistake here. This seems to make sense to me, but I'm not getting anything in the console.

Can someone point me in the right direction?

+5
source share
4 answers

If you provided the same class name for all img tags, try this,

  $(".photo").each(function() {  
   imgsrc = this.src;
   console.log(imgsrc);
  });  
+11
source
$(document).ready(function(){
  $(".photo").each(function() {  
       imgsrc = this.src;
       console.log(imgsrc);
  });    
});
+4
source

lenght / :

var imgsrc = $(this).attr("src").length;

var imgsrc = $(this).attr("src");

: ,

if (imgsrc == undefined) { /* do something */ }
0

, doc ready handler:

$(function(){
  $.each($(".photo"), function() {
    var imgsrc = $(this).attr("src").length;
    console.log(imgsrc); // logging length it should now print the length
  });
});

be sure to download this script first and then your function $.each():

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
0
source

All Articles