Search code examples
javascriptjqueryhtmlimagesrc

How to bypass eg header and its contents and html code by javascript?


I have javascript code that replaces the content of data-src in each img tag with src. I would like to skip, for example, header and its content because I have a slider in it that works like this script only in it turns src into data-src and thus it doesn't work for me. I would like to add the header content to the exceptions so that the script would be everywhere but not in the header and its content

function czLazyload() {
  'use strict';

  j('[data-src]').each(function() {
    if (j(this).data('src') && ScrollIntoView(j(this))) {
      j(this).attr('src', j(this).data('src')).attr('srcset', j(this).attr('old')).removeAttr('data-src old').addClass('lazyloaded');
    }
  });
}

Solution

  • jQuery's .not() should help you here

    function czLazyload() {
      'use strict';
    
      j('[data-src]').not('header').each(function() {
        if (j(this).data('src') && ScrollIntoView(j(this))) {
          j(this).attr('src', j(this).data('src')).attr('srcset', j(this).attr('old')).removeAttr('data-src old').addClass('lazyloaded');
        }
      });
    }
    

    More: https://api.jquery.com/not/