-4

Is it possible to make an html tag a link to another page on website using either css or javascript? I am trying to edit a wordpress site but it only gives options for custom css and javascipt.

Thanks

Adam Katz
  • 6,999
  • 11
  • 42
  • 74

3 Answers3

2

Try this javascript:

document.getElementById("link").onclick = function() {
     window.location.href = "http://stackoverflow.com";
}
<p id="link">Go to Stack Overflow</p>

and with CSS to make it look like an anchor tag:

#link {
    color: blue;
    text-decoration: underline;
}
joe_young
  • 4,107
  • 2
  • 27
  • 38
1

You can add html tags using javascript in a couple ways. The most straightforward way (though it can be dangerous if you put malicious html in), is to use someNode.innerHTML = <some markup>, or if you have jquery: obj.html('');

Knowing that, you can use the html link tag (the <a>) to add a link.

so if I want to put in link to google in my header i'd first select my header: var header = document.getElementById('header');

Then I'd add the a tag to the header: header.innerHTML += '<a href="www.google.com">Google</a>;

Sam Jacobs
  • 346
  • 1
  • 8
0

This is answered at onclick="location.href='link.html'" does not load page in Safari.

onclick="javascript:location.href='http://www.example.com/'"

Community
  • 1
  • 1
thatgibbyguy
  • 4,033
  • 3
  • 23
  • 39