Alir Kahi
Alir Kahi

Reputation: 1300

Conditioning in jQuery selectors

I'm totally new to jQuery and I'm trying to write a simple script in to say I want to add an pdf.png image to all the links that end with .pdf and also a external link that starts with http but I for PDF files on different hosts, I want the script to only add the pdf image (I have a css file for adding the images) like this:

$(document).ready(function(){
    $("a[href$='.pdf']").addClass('pdf');
    $("a[href^='http']a[href$!='.pdf']").addClass('external');   
});

but it actually shows the external image when the path starts with http and ends with .pdf (so it's doing the opposite). Could anyone help me on this?

Upvotes: 2

Views: 247

Answers (2)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

The following code will add the class pdf to the elements which have href ends with .pdf but not starts with http. According to your comment, you just need to add the class external to the elements which are having href starts with http and not ends with .pdf. Have a look at the line of code below the commented one to achieve your need.

Try this,

$(document).ready(function(){
    $("a[href $= '.pdf']").addClass('pdf');
    //$("a[href^='http']a[href$!='.pdf']").addClass('external');   
    $("a[href^='http']").not("a[href $= '.pdf']").addClass('external');   

});

Upvotes: 2

tewathia
tewathia

Reputation: 7298

Try this

$(document).ready(function(){
    $("a[href^='http']").addClass('external'); 
    $("a[href$='.pdf']").removeClass('external').addClass('pdf');  
});

Upvotes: 0

Related Questions