Chad
Chad

Reputation: 5428

Grabbing an image from a page using jQuery

So basically what I want to do is grab links to images within a page and convert them to images. Right now, I have code that will grab any link within a post on the forums and converts them to images, but I want it more specific than that. I want to be able to grab images using the main 4 web image extensions (.jpg, .gif, .png, .bmp). I'm new to jQuery so I'd really love some help on this. Here's my code that I have so far:

(function($) {
    if ($('#nav a[href*="/forum/3828932/"]').length && location.href.indexOf('/1/') !== -1) {
        $('td.c_post:eq(0) a').each(function () {
            link = $(this).attr('href');
            $(this).html('<a href="' + link + '"><img src="' + link + '" alt="Icon" /></a>')
        });
    }
})(jQuery);

Upvotes: 3

Views: 729

Answers (1)

Quincy
Quincy

Reputation: 4433

You can try to use the ends-with selector to select anchor with href ends with those values http://api.jquery.com/attribute-ends-with-selector/

and multiple selector http://api.jquery.com/multiple-selector/

so your selector may look something like

$('td.c_post:eq(0) a[href$=".jpg"],td.c_post:eq(0) a[href$=".png"],td.c_post:eq(0) a[href$=".gif"],td.c_post:eq(0) a[href$=".bmp"]')

Upvotes: 2

Related Questions