5

My HTML looks like this:

<div class="col-md-2" id="myName1">
  <p>
    <a href="/something/121212">Get This Text</a>
  </p>
</div>

The question is how do I get the text "Get this Text"

Something like this, but getting that text which is wrapped in the p and a tags:

function () {
  return document.getElementById('TextID');
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
jonmrich
  • 4,233
  • 5
  • 42
  • 94

6 Answers6

10

You can search for the first p inside your myName1 element, then the first a within that.

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

var theText = e.innerHTML;

console.log(theText);

// or, in sufficiently-modern browsers

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

console.log( theText );
<div class="col-md-2" id="myName1">
<p>
<a href="/something/121212">Get This Text</a>
</p>
</div>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
2

Simply using document.getElementById('anchorID').text; assuming anchor has id of anchorID. The text property sets or returns the text content of a link.

EDIT 1 : If you are not able to add the ID, then you need to take long path by going to document.getElementByID and then reach to the element using the document.getElementsByTagName

  var myAnchor = document.getElementById("myName1").getElementsByTagName('p')[0].getElementsByTagName('a')[0];
  console.log(myAnchor.text);
<div class="col-md-2" id="myName1">
<p>
<a id="anchorID" href="/something/121212">Get This Text</a>
</p>
</div>
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
  • Problem is that there is no `id` on the `a` tag and I can't add one. – jonmrich Feb 01 '17 at 20:22
  • in that case, you can use the `document.getElementById("myName1").getElementsByTagName('p')[0].getElementsByTagName('a')[0]` to get all the anchor elements and then get the first element (assuming you have only 1 anchor tag). Updated the post. – Abhinav Galodha Feb 01 '17 at 20:23
  • As MDN doesn't document `aElement.text`, I was wondering if it is standard HTML; indeed it is: According to https://html.spec.whatwg.org/#dom-a-text , `aElement.text` is the same as `anyElement.textContent`. – Abdull Jul 13 '22 at 09:03
2

Try adding the following in your function:

return document.querySelector('#myName1 p a').innerHTML
Thomas
  • 103
  • 5
1

you can use the get element by tag name method, but it returns an array of results so you will have to consider that, in your example, this works...

var a=document.getElementById('myName1');
console.log(a.getElementsByTagName('p')[0].getElementsByTagName('a')[0].innerHTML);
<div class="col-md-2" id="myName1">
<p>
<a href="/something/121212">Get This Text</a>
</p>
</div>
0

Check this code, you can use innerHtml attribute

<script>
function gettext()
{
    return document.getElementById('link').innerHTML;
}

</script>
 <div class="col-md-2" id="myName1">
<p>
<a href="/something/121212" id="link">Get This Text</a>
</p>
</div>
<script>
alert(gettext());
</script>
Ghanshyam Bagul
  • 169
  • 1
  • 12
0

Or if you are using JQuery $("#myName1 p a").text();

Waleed
  • 349
  • 3
  • 6