2

<a href='http://example.com'>Goes To Example.com</a>

I want to get the href value of this. This link will always be on the page

I will usually do something like: document.getElementsByTagName('a')[5]. However, the number of links are always changing so it isn't reliable at all. Is there a way I can get it by the text Goes To Example.com?

2
  • Is JQuery an option? Commented Aug 27, 2014 at 4:40
  • Yes I can use jQuery Commented Aug 27, 2014 at 4:40

5 Answers 5

2

As you said in comment you can use jquery, use Contains like bellow

$('a:contains("Goes To Example.com")')

DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

Keeps returning null for some reason
@JosephSala try once $('a:contains(Goes To Example.com)') no quotes in contains, in demo both works
Still returns null, works in fiddle but not on localhost
My bad, I forgot to include jQuery. All good solutions but this one was simplest.
1

Use the JQuery :contains() selector. You can then get the attribute "href", as follows:

$("a:contains(Goes To Example.com)").attr("href");

For example, the following snippet will popup an alert with http://example.com inside it:

<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    </head>
    <body>
        <a href='http://example.com'>Goes To Example.com</a>

        <script>
            alert($("a:contains(Goes To Example.com)").attr("href"));
        </script>
    </body>
</html>

Comments

1

You could use xpath:

document.evaluate(
    '/html/body//a[text()='Goes To Example.com']/@href', 
    document, null, XPathResult.ANY_TYPE, null);

You can iterate over the result (which is of type XPathResult) using iterateNext.

See xpath documentation for details: https://developer.mozilla.org/en-US/docs/Web/API/document.evaluate.

2 Comments

SyntaxError: Unexpected identifier
Sorry, I used ... at the end to indicate parameters that you needed to fill in. I've now given the full statement.
0

You can do document.getElementById and give the id to the a tag. To get the text you can use innerHTML function

Working Fiddle

HTML Markup

<a id="test" href='http://example.com'>Goes To Example.com</a>

JS:

document.getElementById("test").innerHTML

If you want it by href attribute only then you have to loop it over

var els = document.getElementsByTagName("a");
for (var i = 0, l = els.length; i < l; i++) {
    var el = els[i];
    if (el.href === 'http://www.example.com/') {
        alert(el.innerHTML);
    }
}

1 Comment

He mentioned to get id value based on its text value, not by its herf value. Got it.
0

You can do it like this:

var links = document.getElementsByTagName('a');
var requiredId = "";
for(var i=0; i<links.length; i++){
    if(links[i].innerHTML == "Goes To Example.com"){
        requiredId = links[i].getAttribute('id');
    }
}

2 Comments

How is it different from my answer?
Here, if 'a' elements's text is equal to 'Goes To Example.com' then am taking its attribute value. But, not href based. This is what question author has mentioned. Please check it once in question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.