I'm writing jQuery code where I need to check if link is an image. Currently code looks like this:
$('a[href$=".png"]').click(function() {
// do something smart
});
Is it possible to have an array of attributes in the code, to check for multiple formats, something like this:
$('a[href$=.jpg],a[href$=.jpeg],a[href$=.png]').click(function() {
// do something even smarter
});
What is the best practice here? Thanks!
Yes you can do that.
That is perfectly legal.
What you can do also is check on click what the href is:
$('a').click(function(){
var href = this.href;
var hrefParts = href.split('.'); //array(0: fileName, ...length - 1: extension)
if(hrefParts[hrefParts.length - 1] == 'png'){...}
//etcetera...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With