Search code examples
javascriptjqueryimagehrefprop

Change the Image Link to "#" using jQuery


I validate image path whether it is image path or some other path. If it is the image then replace the particular href value to # But the # value is affected all links. How can i replace the image link to #

jQuery(document).ready(function($) {
   $("a").each(function(i, el) {
      var href_value = el.href;
      if (/\.(jpg|png|gif)$/.test(href_value)) {
         jQuery('a').prop('href', '#');
      }
   });
});

Here is the

FIDDLE

Any Suggestion would be great.


Solution

  • use this

    jQuery(this).prop('href', '#');
    

    or

    jQuery(el).prop('href', '#');