6

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'); } 
1

6 Answers 6

11

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>

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

2 Comments

In this specific case, he doesn't even need to search for the first p.
No, but the question specified "that text which is wrapped in the p and a tags", so we had to pick a p element to search within.
2

Try adding the following in your function:

return document.querySelector('#myName1 p a').innerHTML 

Comments

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>

3 Comments

Problem is that there is no id on the a tag and I can't add one.
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.
As MDN doesn't document aElement.text, I was wondering if it is standard HTML; indeed it is: According to html.spec.whatwg.org/#dom-a-text , aElement.text is the same as anyElement.textContent.
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>

Comments

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> 

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.