'How to make an image as a clickable link to another html page using javascript?

This is the JavaScript code I wrote :

var goToSecondPage= document.querySelector('.mobile');

goToSecondPage.addEventListener("click", function() {

document.location.href='productDetails.html';
 })

I'm trying to use an image in an existed class on an index html page (called mobile) and make it as a clickable link image to another html file called productDetails.html



Solution 1:[1]

It depends where your productDetails.html file is located. If it's in the root directory try

var goToSecondPage = document.querySelector('.mobile');

goToSecondPage.addEventListener("click", function() {
    document.location.pathname = '/productDetails.html';
});

This will only change your path, not the entire url.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marian