Jump to content

How to link image to link in JavaScript

Mattattack94
Go to solution Solved by Mattattack94,

I figured it out, here is the answer incase someone else has the problem:

 

var img = new Image();
img.src = 'logo.png';
img.id = "logo"
img.height = "56"
img.onclick = function() {
    window.location.href = 'Html.html';
};
document.body.appendChild(img);

 

I am trying to convert an html website into one made entirely of JavaScript and CSS. However, I am having trouble linking an image to a URL in JS. Here is the code I'm having trouble with: 

	var a = document.createElement('a');
    var img = document.createElement('img');
    a.href = "Html.html";
    img.appendChild = "a";
    img.src = "logo.png";
    img.title = "Daring Fireball: Home";;
    img.id = "logo";
    img.alternate = "Daring Fireball"
    img.height = "56"
    img.href = "Html.html";
    document.body.appendChild(img);

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Mattattack94 said:

I am trying to convert an html website into one made entirely of JavaScript and CSS. However, I am having trouble linking an image to a URL in JS. Here is the code I'm having trouble with:

Several things wrong with that. First of all, a - elements define links, so if you want an img to be a clickable link, it has to be the child-element of a, not the other way around. Secondly, you're assigning the string "a" as a child to img, instead of the variable a, so it wouldn't work anyways.

    var a = document.createElement('a');
    var img = document.createElement('img');
    a.href = "Html.html";
    a.appendChild = img;
    img.src = "logo.png";
    img.title = "Daring Fireball: Home";
    img.id = "logo";
    img.alternate = "Daring Fireball"
    img.height = "56"
    document.body.appendChild(a);

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

I figured it out, here is the answer incase someone else has the problem:

 

var img = new Image();
img.src = 'logo.png';
img.id = "logo"
img.height = "56"
img.onclick = function() {
    window.location.href = 'Html.html';
};
document.body.appendChild(img);

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

4 minutes ago, WereCatf said:

Several things wrong with that. First of all, a - elements define links, so if you want an img to be a clickable link, it has to be the child-element of a, not the other way around. Secondly, you're assigning the string "a" as a child to img, instead of the variable a, so it wouldn't work anyways.

Thank you for the explanation, I see what I did wrong but when I tried you code it did not load the image. I was able to modify my code and get it working, however.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×