Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use an array for attribute comparison in jQuery?

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!

like image 390
Klikerko Avatar asked Dec 31 '25 20:12

Klikerko


1 Answers

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...
});
like image 64
Naftali Avatar answered Jan 02 '26 09:01

Naftali



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!