Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find if link targets image with javascript/jquery

Tags:

jquery

regex

I am trying to write a script that will find all <a> tags whose target is either a jpg, gif, or png and attach a function to them.

$('a')
    .filter(function(){
        return this.href.match(/*probably some regex here?*/)
    })
    .bind('mouseover', function(){
        alert('foo');
    })

This should work, but I don't know what the regex would look like. If there's a better way, please let me know that as well. Thanks!

like image 510
Chris Sobolewski Avatar asked Sep 11 '26 00:09

Chris Sobolewski


1 Answers

You almost had it!!!

$('a[href]').filter(function() {
  return /(jpg|gif|png)$/.test($(this).attr('href'))
}).bind('mouseover', function(){
  alert('foo');
})
like image 54
Pastor Bones Avatar answered Sep 12 '26 15:09

Pastor Bones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!