tags:

views:

29

answers:

2
function openFile(file) {
    var extension = file.substr( (file.lastIndexOf('.') +1) );
    switch(extension) {
        case 'jpg':
        case 'png':
        case 'gif':
            alert('was jpg png gif');  
        break;                         
        case 'zip':
        case 'rar':
            alert('was zip rar');
        break;
        case 'pdf':
            alert('was pdf');
        break;
        default:
            alert('who knows');
    }
};

openFile("somestring.png");

I got this piece a code from another question posted here, but im unsure how to implement it for my purpose. I want to check the hrefs of every link and place the appropriate icon for the file type.

A: 
$('a').each(function () {
    openFile($(this).attr('href'));
});
Skilldrick
+4  A: 

Use .each function:

$("a").each(function() { openFile($(this).attr('href')); }

It allows you to iterate through the matched sets of elements.

Xion
thanks. However, for some reason it's not working for me.
Adam
it must be me. I'm trying simple functions using the .each iteration and it won't work. Any ideas as to why it's not working?
Adam
ah ok sorry. I guess javascript needs the code in order
Adam