document.getElementsByClassName('reply-link')
returns a node list. Node lists don't have a .getAttribute()
method. You need to select a node from the list and work with that.
You can do that by passing an index to the returned node list as shown here:
var pick = document.getElementsByClassName('reply-link')[0].getAttribute('href');
alert(pick);
<div class="metadata">
<span class="type">
<a name="txn-777" href="Display.html?id=12345#txn-777">#</a>
</span>
<span class="date">Fr 10. Aug 11:12:29 2018</span>
<span class="description">
<span class="user" data-user-id="31"><a href="/homepage/User/Summary.html?id=31">admin</a></span>created
</span>
<span class="time-taken"></span>
<span class="actions">[<a href="Update.html?id=12345&QuoteTransaction=777&Action=Respond" class="reply-link">Antworten</a>] [<a href="Update.html?id=12345&QuoteTransaction=777&Action=Comment" class="comment-link">Kommentieren</a>] [<a href="Forward.html?id=12345&QuoteTransaction=777" class="forward-link">Weiterleiten</a>]</span>
But, there are two reasons not to use .getElementsByClassName()
in the first place.
It returns a "live" node list that requires that the entire DOM be re-queried every time the pick
variable is accessed so that it can always contain a collection of the most current nodes. This is very wasteful in terms of performance and should only be considered in situations where the DOM dynamically changes frequently.
You're scanning the entire DOM, making a collection of all matching nodes and then throwing all but one of them away. Again wasteful.
Instead, use .querySelector()
to get a single node that matches a CSS selector.
Also, while .getAttribute()
perfectly fine, you can also access any HTML attribute as a DOM object property.
alert(document.querySelector('.reply-link').href);
<div class="metadata">
<span class="type">
<a name="txn-777" href="Display.html?id=12345#txn-777">#</a>
</span>
<span class="date">Fr 10. Aug 11:12:29 2018</span>
<span class="description">
<span class="user" data-user-id="31"><a href="/homepage/User/Summary.html?id=31">admin</a></span>created
</span>
<span class="time-taken"></span>
<span class="actions">[<a href="Update.html?id=12345&QuoteTransaction=777&Action=Respond" class="reply-link">Antworten</a>] [<a href="Update.html?id=12345&QuoteTransaction=777&Action=Comment" class="comment-link">Kommentieren</a>] [<a href="Forward.html?id=12345&QuoteTransaction=777" class="forward-link">Weiterleiten</a>]</span>