Adding link to a clickable HTML image with a link that will redirect to another webpage using JAVASCRIPT

174 Views Asked by At

I don't know how to make an HTML image clickable. Also, using Javascript, I want to know how to make a clickable HTML image redirect to another webpage when clicked. For example:

the HTML code of the image that I want to be clickable

Also, how to redirect to another webpage when the image is clicked. Like this:

This, when this is clicked I want it to redirect us to another webpage

Can someone please help me, I will appreciate any answers.

I tried doing this:

$('#imagefour').click(function() {
  window.location.href = "https://en.wikipedia.org/wiki/Assassination_Classroom";
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="">
  <img src="https://i.ytimg.com/vi/GY9FYJ-OG0I/maxresdefault.jpg" id="imagefour">
</a>

but nothing happened when I clicked the image, it didn't work. The school requires us to really use Javascript because it is the main goal of the project, thank you!!!

1

There are 1 best solutions below

0
On

Put the click function inside document.ready