-1

I want to get the anchor (here "linktext") of a textlink on a special page:

<div id="msgbox"><h2>xxx</h2><p>xxxx <a href="./url">linktext</a>xxx</p></div>

I tried it with Javascript to get link text and this code:

var e = document.getElementById('msgbox').
        getElementsByTagName('p')[0].
        getElementsByTagName('a')[0];

var theText = e.innerHTML;

console.log(theText);

// or, in sufficiently-modern browsers

e = document.querySelector('#msgbox p a');
theText = e.innerHTML;

return theText ;
}

But i just get something like

"HTMLDivElement: html > body.sliding-popup-processed > div"

as value. There can be more than one linktext on the page, which should be tracked.

Can you help me out?

RBT
  • 21,293
  • 19
  • 144
  • 210
vinc3nt
  • 1
  • 1
  • It sounds like you are just looking for [querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) and [textContent](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) – DBS May 20 '22 at 09:01
  • Your code, *as provided* works fine: https://jsfiddle.net/wvr2t519/ It's unclear what `return inhalt` is meant to do or where you're getting your "something like this" - please [edit] your question and include a *snippet* that *demonstrates* the issue. – freedomn-m May 20 '22 at 09:04
  • 1
    Thank you for the hint! It was a copy/paste error, i deleted the wrong "return" and now it works :) Thanks a lot and sorry for the dumb error ;) – vinc3nt May 20 '22 at 09:43

0 Answers0