0

I am using blazy image loader and I am trying to get the data src from the element on error. Basically if the image is not found or invalid I want to change the last 4 letters of the image src, but I cannot figure out how to get the actual source. Here is my code:

var bLazy = new Blazy({
  loadInvisible: true,
  error: function (ele, msg) {
     if (msg === 'missing') {
        var dsrc = ele.src();
        var fallback = dsrc.replace('y.jpg','b.jpg');
        ele.src(fallback);
     } else if (msg === 'invalid') {
        console.log(this);
     }
  },
  success: function(ele,msg){
      //console.log(ele.parseJSON());
      console.log(this);
  }
   });

I also tried ele.attr('data-src'); and that didn't work either. When I look at the console for console.log(ele) I get the actual image not the element.

Anyone who could help... please!!

lov2code
  • 394
  • 1
  • 6
  • 19

1 Answers1

1

If you would like to get the "src" attribute, you can use the $(ele).data("src") or $(ele).attr("data-src") with JQuery. After you can modify.

  ;(function() {
      // Initialize
      var bLazy = new Blazy({
        error: function(ele, msg){
          if(msg === 'missing'){
            // Data-src is missing
            console.log(msg);
          } else if(msg === 'invalid'){
            // Data-src is invalid
            //console.log( $(ele).data("src") );
            var src = $(ele).data("src");
            src = src.substring(0, src.length - 4) + 'b.jpg';
            //or
            //src = src.replace('y.jpg','b.jpg');
            $(ele).attr("src",src);
          }  
        }          
      });
  })();
Michael
  • 425
  • 3
  • 13