dero
dero

Reputation: 11

jquery file extensions

i need one thing to do:

<li class="pdf"><a href="#">failanosaukums.pdf</a></li>
<li class="excel"><a href="#">failanosaukums.exel</a></li>
<li class="word"><a href="#">failanosaukums.doc</a></li>

If file exstensions are pdf, add li class pdf, and doc, exel, mp3.

I think something like this

var ext = ul li a each split (.) 
$(this).parent().addClass(ext)

Upvotes: 0

Views: 130

Answers (3)

Richard Dalton
Richard Dalton

Reputation: 35793

I agree with everybody else that server side is the way to go, but if you must do it client side:

$('.downloads li').addClass(function() { 
    return $('a', this).text().split('.').pop();
});

Example - http://jsfiddle.net/RNvnL/3/

Upvotes: 0

Chris
Chris

Reputation: 4471

If you can't do this server side when generating the rest of the html, this should work

$("ul.files a").each(function() {
    var $this = $(this);
    var text = $this.text();
    var ext = text.substring(text.lastIndexOf('.') + 1);

    $this.parent().addClass(ext);
});

Upvotes: 2

CassOnMars
CassOnMars

Reputation: 6181

I'm not really sure if what you're wanting is a good idea, but this will do it for you.

var files = $("li a");

files.each(function(idx, el) { $(el).parent().addClass($(el).text().split(".").pop()) });

Upvotes: 1

Related Questions