0
 <img src="linkclick.aspx?fileticket=0" id="directory-image">

Let's say when this comes up, I want to replace "linkclick.aspx?fileticket=0" with "images/no-avatar.gif"

I assume this can be done with Jquery, but I can't easily figure it out.


<script type="text/javascript">

function replaceIMGText() {

var imgsrc = $("img#directory-image").attr("src");
if imgsrc = "linkclick.aspx?fileticket=0" {
imgsrc="images/no-avatar.gif"
}
else {}
$("img#directory-image").attr("src", imgsrc)

}


$(document).ready(replaceIMGText);
$("html").ajaxStop(replaceIMGText);
</script>

Any help appreciated!

Goomba
  • 1
  • 1

2 Answers2

1

This is a Javasript syntax error:

if imgsrc = "linkclick.aspx?fileticket=0"

You need parenthesis:

if(imgsrc = "linkclick.aspx?fileticket=0")

Then you have a semantic error, you also need to change your = to ==:

if(imgsrc == "linkclick.aspx?fileticket=0")

You can also remove else {} if you want, since it does nothing, and you should add semicolons after:

imgsrc="images/no-avatar.gif"

and:

$("img#directory-image").attr("src", imgsrc)
Paul
  • 139,544
  • 27
  • 275
  • 264
  • @Goomba - You can remove the else {} statement aswell since it's empty – Tom Mar 03 '12 at 20:32
  • Thanks, I tried the following and it works but it replaces ALL images with ID# directory-image with no-avatar.gif... including the ones that are actual images. – Goomba Mar 04 '12 at 16:06
  • Can anyone explain why this won't work: $("img#directory-image").attr("src").replace("linkclick.aspx?fileticket=0", "images/no-avatar.gif"); Do I need to call this function somehow? – Goomba Mar 04 '12 at 16:25
  • @Goomba That doesn't work because the `attr('src')` just returns a string. Modifying that string doesn't modify the attribute. You need to set the attribute to the new value of the string using for example `attr("src", "newvalue")`. – Paul Mar 04 '12 at 17:38
  • @Goomba As for your other problem, an HTML document cannot have more than one element with the same ID. If you have two or more elements with the same ID it's no longer HTML. You have something similar to HTML, but it ins't really HTML. Usually if your in a situation where you have more than one element with the same ID you should've been using classes instead of ids. – Paul Mar 04 '12 at 17:40
0

Actually this solution worked great without any jQuery:

jQuery/JavaScript to replace broken images

function ImgError(source){
  source.src = "/images/noimage.gif";
  source.onerror = "";
  return true;
}

<img src="someimage.png" onerror="ImgError(this);"/>
Community
  • 1
  • 1
Goomba
  • 1
  • 1