Goomba
Goomba

Reputation: 1

Jquery to replace SRC for img tag

 <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!

Upvotes: 0

Views: 2131

Answers (2)

Goomba
Goomba

Reputation: 1

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);"/>

Upvotes: 0

Paul
Paul

Reputation: 141927

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)

Upvotes: 1

Related Questions