HTML link with href that refers to text?
Asked Answered
C

5

30

This is a simple yes or no question (probably no), googling didn't seem to give me a straight answer. Say you have a link that is

<a href="www.stackoverflow.com">www.stackoverflow.com</a>

Is it possible to make something like

<a href=self.text>www.stackoverflow.com</a>

without using anything else (obviously, no scripts)? Is there any kind of shortcut?

Crossland answered 18/9, 2012 at 21:8 Comment(3)
Nope, no way to do that.Shagreen
That's only possible with JavaScript, I'd imagine. Though, with CSS you could sort of do it the other way round, take the href attribute and make that the link text. (Though it wouldn't generate a clickable text, I think.)Typhon
@DavidThomas - A quick test shows that it generates clickable text in IE, Firefox, Opera and Safari, but not Chrome.Yelmene
H
19

No, you need to have both the href attribute and a value between the a tags. Without using any scripts, it's not possible to refer to its own contents.

Hughes answered 18/9, 2012 at 21:10 Comment(0)
M
0

create an script/function for that

put a class to all of your anchors (i.e. "anchorTextAsHref" ), get all of them and modify the DOM structure,

just placing the text equals to whatever href has.

    let anchors=document.getElementsByClassName("anchorTextAsHref");
    for (let i = 0; i < anchors.length; i++) {
        anchors[i].text = anchors[i].href;
    }
Moulmein answered 23/3, 2023 at 16:28 Comment(0)
S
0

You can do it the other way around.

Use the CSS pseudo class a::before and set its content to attr(href).

a {
font-family: sans-serif;
font-size: 20px;
}

a::before {
  display: block;
  content: attr(href);
}
<a href="http://stackoverflow.com"/>
Suffer answered 23/6 at 1:49 Comment(0)
P
-2

Use anchor link Useful Tips Section Create a link to the "Useful Tips Section" inside the same document:

Visit the Useful Tips Section

Pocketful answered 18/9, 2012 at 21:55 Comment(0)
D
-3

I think you're looking for the Anchor Tag . You can use the syntax to link to it, if you reference it by using a <a href="link"> somewhere in the body.

Look here at the bottom for some examples:

http://www.w3schools.com/html/html_links.asp

Double answered 18/9, 2012 at 21:12 Comment(2)
Nope, not looking for an anchor. Just looking for a way to get the href to equal the text. Thanks, though.Crossland
I was simply wondering if it was possible (without Javascript) to have a link's URL lifted from its text. The answer is no.Crossland

© 2022 - 2024 — McMap. All rights reserved.