Thanks. This helped reduce the code but still not perfect. Here is my present code which works but still I cannot eliminate some redundant code.
$("a[href$='.doc']:not(a[href^='http']),a[href$='.xls']:not(a [href^='http']),a[href$='.ppt']:not(a[href^='http']),a[href $='.pdf']:not(a[href^='http']),a[href$='.mpg']:not(a [href^='http'])").each(function(){ var ext=$(this).attr("href").match(/\.[^\.]*$/); if (ext) $(this).after($('<span/>').attr ('class','fileExtension').html(" (" + ext + ") ")); } ); I have modified your code as I was getting errors due to a variety of links on my page (links with neither url value for href nor any extension etc.,). In the above code as you see I had to append :not(a[href^='http']) to all different kinds of file extensions I am interested in. I tried the following to eliminate the redundant code but it does not work. $("a:not[href^='http']").find("a[href$='.doc'],a[href$='.docx'],a[href $='.xls'],a[href$='.xlsx'],a[href$='.ppt'],a[href$='.pptx'],a[href $='.pdf'],a[href$='.mpg']").each(function(){ var ext=$(this).attr("href").match(/\.[^\.]*$/); if (ext) $(this).after($('<span/>').attr ('class','fileExtension').html(" (" + ext + ") ")); } ); Thanks On May 15, 11:31 am, Jason Persampieri <papp...@gmail.com> wrote: > Is this what you're looking for? > > $("a:not[href^=http]").each(function(){ > var ext=$(this).attr("href").match(/\.[^\.]*$/); > if (ext) $(this).after("<span class='fileExtension'>("+ext.slice > (1)+")</span>"); > > } ); > > On May 15, 8:09 am, infoaddicted <jack.lapla...@gmail.com> wrote: > > > > > This calls for a function, where you can hide the gory details. Use a > > selector to gather up the "a" elements, and pass that as the argument > > to the function. > > pseudocode: > > > var linksToChange = JQuery Selector; > > function changeLinks( linksToChange ){ > > > either: > > a "for" loop to step through the object links ToChangelike an > > array > > probably an "if" conditional in every iteration > > see:http://tinyurl.com/3sdkgl > > also check out jquery's .each() iterator in the Utilities > > section of the docs > > > or: > > a "switch" statement > > see:http://tinyurl.com/osezq2 > > > } > > > On May 14, 3:18 pm, vmrao <maheshpav...@gmail.com> wrote: > > > > OK. I was able to add the text as well as apply special style to it > > > using the following code. > > > > $("a[href$='.doc']:not(a[href^='http'])").after($('<span/>').attr > > > ('class','fileExtension').html(" (" + '.doc' + ") ")); > > > $("a[href$='.docx']:not(a[href^='http'])").after($('<span/>').attr > > > ('class','fileExtension').html(" (" + '.docx' + ") ")); > > > $("a[href$='.xls']:not(a[href^='http'])").after($('<span/>').attr > > > ('class','fileExtension').html(" (" + '.xls' + ") ")); > > > $("a[href$='.xlsx']:not(a[href^='http'])").after($('<span/>').attr > > > ('class','fileExtension').html(" (" + '.xlsx' + ") ")); > > > $("a[href$='.ppt']:not(a[href^='http'])").after($('<span/>').attr > > > ('class','fileExtension').html(" (" + '.ppt' + ") ")); > > > $("a[href$='.pptx']:not(a[href^='http'])").after($('<span/>').attr > > > ('class','fileExtension').html(" (" + '.pptx' + ") ")); > > > $("a[href$='.pdf']:not(a[href^='http'])").after($('<span/>').attr > > > ('class','fileExtension').html(" (" + '.pdf' + ") ")); > > > $("a[href$='.mpg']:not(a[href^='http'])").after($('<span/>').attr > > > ('class','fileExtension').html(" (" + '.mpg' + ") ")); > > > > However, I am not sure if there is scope to minimize the above code > > > (as you see one line for every file extension). > > > > On May 14, 2:42 pm, vmrao <maheshpav...@gmail.com> wrote: > > > > > I think it is not clear. Here is my code. > > > > > $("a[href$='.doc']").after(" (" + '.doc' + ") "); > > > > $("a[href$='.docx']").after(" (" + '.docx' + ") "); > > > > $("a[href$='.xls']").after(" (" + '.xls' + ") "); > > > > $("a[href$='.xlsx']").after(" (" + '.xlsx' + ") "); > > > > $("a[href$='.ppt']").after(" (" + '.ppt' + ") "); > > > > $("a[href$='.pptx']").after(" (" + '.pptx' + ") "); > > > > $("a[href$='.pdf']").after(" (" + '.pdf' + ") "); > > > > $("a[href$='.mpg']").after(" (" + '.mpg' + ") "); > > > > > I would like to know if it is possible to have a one-line code which > > > > replaces all the above code. Also, for the extra text (ex: .doc) being > > > > added, I need to add a special CSS style. I tried this and it does not > > > > work. > > > > > $("a[href$='.doc']").after(" (" + '.doc' + ") ").addClass > > > > ("fileExtension"); > > > > > On May 14, 2:00 pm, waseem sabjee <waseemsab...@gmail.com> wrote: > > > > > > here i modified my code for class adding > > > > > > var obj = $("a"); > > > > > > for(var i = 0; i < obj.length; i++) { > > > > > obj.eq(i).wrap("<span></span>" > > > > > > > ); // wrap the hyperlink in a span tag > > > > > > obj.parent().eq(i).prepend('<span class="before">This is text > > > > > > before the > > > > > > hyperlink</span>'); > > > > > > obj.parent().eq(i).append('<span class="after">This is text after > > > > > > the > > > > > > hyperlink</span>'); > > > > > > } > > > > > > On Thu, May 14, 2009 at 7:58 PM, waseem sabjee > > > > > <waseemsab...@gmail.com>wrote: > > > > > > > var obj = $("a"); > > > > > > > for(var i = 0; i < obj.length; i++) { > > > > > > obj.eq(i).wrap("<span></span>"); // wrap the hyperlink in a span tag > > > > > > obj.parent().eq(i).prepend("This is text before the hyperlink"); > > > > > > obj.parent().eq(i).append('This is text after the hyperlink'); > > > > > > } > > > > > > > use the above common method and you can furthur change this script > > > > > > to look > > > > > > for hyperlinks with a specific class by saying : > > > > > > var obj = $("a.myclass"); > > > > > > or > > > > > > var obj = $("a#myid"); > > > > > > may be even > > > > > > var obj = $("h2 a"); > > > > > > > On Thu, May 14, 2009 at 7:46 PM, vmrao <maheshpav...@gmail.com> > > > > > > wrote: > > > > > > >> Also, I would like to apply a specific CSS style to the text being > > > > > >> added. > > > > > > >> Here is my CSS. > > > > > >> .fileExtension { > > > > > >> padding-left:0pt; > > > > > >> font-family: Arial, Helvetica, sans-serif; > > > > > >> font-size: 7pt; > > > > > >> font-weight: normal; > > > > > >> color: #354963; > > > > > >> } > > > > > > >> On May 14, 1:18 pm, vmrao <maheshpav...@gmail.com> wrote: > > > > > >> > Thanks. How about if I want to make it dynamic ? Say, I have > > > > > >> > several > > > > > >> > links on the page with different extensions > > > > > >> > (Ex: .doc, .docx, .ppt, .pptx, .pdf, .mpg). Can I accomplish the > > > > > >> > above > > > > > >> > with one statement rather than hard coding what text to append > > > > > >> > for > > > > > >> > each extension ? > > > > > > >> > On May 14, 12:23 pm, brian <bally.z...@gmail.com> wrote: > > > > > > >> > > $("a[href$='.pdf']").after(' (pdf)'); > > > > > > >> > > On Thu, May 14, 2009 at 11:55 AM, vmrao > > > > > >> > > <maheshpav...@gmail.com> > > > > > >> wrote: > > > > > > >> > > > I would like to display file extension along with the file > > > > > >> > > > links on > > > > > >> a > > > > > >> > > > web page. > > > > > > >> > > > For example, I would like to append (pdf) next to any pdf > > > > > >> > > > links. > > > > > > >> > > > The following code works to some extent but not as I > > > > > >> > > > intended. > > > > > >> > > > $("a[href$='.pdf']").append(" (" + 'pdf' + ") "); > > > > > > >> > > > If I use the above code, (pdf) is also underlined being part > > > > > >> > > > of the > > > > > >> > > > link. I want (pdf) next to the link and not as part of the > > > > > >> > > > link.- > > > > > >> Hide quoted text - > > > > > > >> > > - Show quoted text -- Hide quoted text - > > > > > > >> > - Show quoted text -- Hide quoted text - > > > > > > - Show quoted text -- Hide quoted text - > > > > > - Show quoted text -- Hide quoted text - > > - Show quoted text -