0

I'm storing an image value in the variable first_img:

var first_img = $('img[alt="example"]').attr('src');

And I want to set the value null after the button is clicked JavaScript or jQuery.

How can I do this?

1
  • Are you trying to remove the image or just do what you're asking, set first_img to null ? Commented Aug 28, 2012 at 11:32

4 Answers 4

1

You could remove the src attribute when the image is clicked:

$('img[alt="example"]').click(function() {
    $(this).removeAttr('src');
});

or completely remove the <img> tag from the DOM:

$('img[alt="example"]').click(function() {
    $(this).remove();
});
Sign up to request clarification or add additional context in comments.

1 Comment

note: $(this).removeAttr('src') doesn't work, see my jsfiddle @ jsfiddle.net/KooiInc/duDxb
1

Not sure I get this at all, as it sounds kinda obvious?

var first_img = $('img[alt="example"]').attr('src');

$('button').on('click', function() {
    first_img = null;
});

Comments

1
var first_img = $('<img alt="example"/>').attr('src',[...] )
 .on('click',
      function(){$(this).remove()}
  );​

see jsfiddle

Comments

1
Try this:-`enter code here`
***$('img[alt="example"]').click(function() {
    $(this).removeAttr('src');
});***

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.