lpoole
lpoole

Reputation: 37

Adding jQuery to js code

I wanted to make this statement using jQuery but cant quite get it to work. In this statement I am clicking on an image in my html and changing it to a new image (img/HeaderImg2.jpg) Please help.

  var imageButton = document.getElementById('headerImg');
  imageButton.addEventListener('click', function(e)
  {
   console.log(e);
   imageButton.src ='img/HeaderImg2.jpg';
  } ,false);

Upvotes: 0

Views: 53

Answers (1)

Felix
Felix

Reputation: 38102

Try this:

$('#headerImg').on('click',function(e) {
    console.log(e);
    $(this).attr('src','img/HeaderImg2.jpg');
});

Btw, it's better to use vanilla javascript other than using jQuery if you're able to do so.

Upvotes: 3

Related Questions