21

I want to get the src of one image and set it as another's src when #btn is pressed:

jQuery("#btn").live("click", function() {
    jQuery("#another_div")
        .children("img")
        .attr("src", jQuery(this).prev("img").attr("src"));
    jQuery(this)
        .prev("img")
        .attr("src","");
})

I have also tried:

jQuery(this).prev('img').removeAttr("src");

It works fine in Firefox, IE7-9, and Safari.

Only in Chrome, the image is not removed even when the source changes (src="").

ZachB
  • 13,051
  • 4
  • 61
  • 89
user883749
  • 213
  • 1
  • 2
  • 5

7 Answers7

31

You could change it to

jQuery(this).attr("src", "data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==");

maybe?

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • Hi Joseph, it seems this works when you do this. Can you explain what is this you have put for the src value? See http://jsfiddle.net/K7SBx/1/ – Manjula Dec 08 '11 at 04:13
  • 6
    Ok I got it. it is a base64 encoded string for a 1px by 1px transparent GIF image. – Manjula Dec 08 '11 at 04:16
  • @ManjulaWeerasinge You are correct. :) I should have explained that. XD Sorry, I did not see your question until this morning. – Joseph Marikle Dec 08 '11 at 13:10
3

Since in my case by doing this

$(this).attr("src","");

clearing the src attribute, but, image does not vanish. Hence I just set, img tag style attribute display to none, like this

$(this).attr("src","").css("display", "none");

It will hide the image. I hope, it will temporarily work for you.

Er. Dj
  • 31
  • 1
2

The following works well with Chrome and does not seem to give any messages to console:

image.src = "about:blank"; // Clear source path
Ari Okkonen
  • 165
  • 1
  • 10
2

try remove image and append it again

jQuery('#another_div').children("img").remove()
jQuery('#another_div').append('<img>').attr("src",jQuery(this).prev('img').attr("src"));
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
1
jQuery('#btn').live("click", function() {
    jQuery('#another_div')
        .children("img")
        .attr("src", jQuery(this).prev('img').attr("src"));
    jQuery(this)
        .prev('img').remove();
})
Terry
  • 14,099
  • 9
  • 56
  • 84
0

This worked for me:

var $image = $('.my-image-selector');
$image.removeAttr('src').replaceWith($image.clone());

However, be wary of any event handlers that you might have attached to the image. There's always the withDataAndEvents and deepWithDataAndEvents options for jQuery clone, but I haven't tested the code with them: http://api.jquery.com/clone/#clone-withDataAndEvents

Also, if you want to do this for multiple images, you will probably have to use something like jQuery each.

I posted an identical answer on a similar question: Clear an IMG with jQuery

Community
  • 1
  • 1
Vlad Sabev
  • 592
  • 1
  • 8
  • 22
0

try to set the src first to blank

$(this).attr("src","");

then

$(this).removeAttr("src");
Netorica
  • 18,523
  • 17
  • 73
  • 108
  • Warning: Do not do this. This will cause an additional server request! http://www.bennadel.com/blog/2236-empty-src-and-url-values-can-cause-duplicate-page-requests.htm – Mister Oh Jan 07 '15 at 21:04