Chirag Patel
Chirag Patel

Reputation: 1611

How to set null value to variable using JavaScript or jQuery?

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?

Upvotes: 0

Views: 10884

Answers (4)

Sunil Marwaha
Sunil Marwaha

Reputation: 123

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

Upvotes: 1

KooiInc
KooiInc

Reputation: 122906

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

see jsfiddle

Upvotes: 1

adeneo
adeneo

Reputation: 318212

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;
});

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038840

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();
});

Upvotes: 1

Related Questions