Bogdacutu
Bogdacutu

Reputation: 771

Find images by absolute path in jQuery

I tried:

$("img[src='"+imgSrc+"']")

but it did not work. imgSrc is an absolute path, but the path in the HTML file is relative. Knowing that I can't modify imgSrc, how can I find images by their absolute path?

Upvotes: 1

Views: 787

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074335

The only thing I can think of is finding the img elements and looking at their src property (rather than attribute; they're different for images with relative paths) to pick out the one(s) you want, e.g.:

$("img[src]").each(function() {
    // Here, `this.src` will be the absolute path
    if (this.src === imageSrc) {
        // Do something, and optionally `return false` to break the
        // loop if you're done
    }
});

Live example

Upvotes: 3

Emre Erkan
Emre Erkan

Reputation: 8482

Browsers handles paths different. You can use $ in your selector to find images ends with imgSrc if it solves your problem.

$("img[src$='"+imgSrc+"']")

Upvotes: -1

Related Questions