Question

I am trying to select an image, and then replace that image with a div and an alternate image and div within that div, but to no avail.

$("img[src='/brand/logo.png']").replaceWith("
    <div class=\"brander\">
        <img src=\"https:\/\/cache.graphicslib.brand.com/new-logo.png\">
        <div class=\"value-propositions\">Low price guarantee!</div>
    </div>
");

Nothing appears to be happening, however, and I have tested to see if the selector is finding the image (with the code below), which it is. I am not sure if the selector is not selecting the entire image tag, or if I am making an error elsewhere.

if ($("img[src='/brand/logo.png']").length) {
    alert ("The selector works!");
}

Any input is welcome. I'm fairly new to JS so, I happily welcome all criticism and advice.

Was it helpful?

Solution

It looks like your selector may not be working.

var derp = $('img[src$="brand/logo.png"]').replaceWith(" \ <div class=\"brander\"> \ <img src=\"https:\/\/cache.graphicslib.brand.com/new-logo.png\"> \ <div class=\"value-propositions\">Low price guarantee!</div> \ </div>");

img[src$= is src 'ends with' may be helpful. (note the \ and new line, that is how you can line break like that in javascript)

http://jsfiddle.net/Mutmatt/TcE5c/12/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top