2

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?

mcuadros
  • 4,098
  • 3
  • 37
  • 44
Alir Kahi
  • 1,264
  • 2
  • 15
  • 27
  • what do you mean by `add an pdf.png image to all the links that end with .pdf and also a external link that starts with http but I don't want for PDF files on different hosts,` – Arun P Johny Dec 07 '13 at 09:38
  • you want to add teh class `pdf` to anchors with target ending with `pdf` and class external to anchors with target starting with `http` but not ending with `http` – Arun P Johny Dec 07 '13 at 09:39

2 Answers2

2

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');   

});
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

Try this

$(document).ready(function(){
    $("a[href^='http']").addClass('external'); 
    $("a[href$='.pdf']").removeClass('external').addClass('pdf');  
});
tewathia
  • 6,890
  • 3
  • 22
  • 27