0

Lets say I have

<a href="/example1">ThisTextChanges</a>
<a href="/example2">ThisTextChanges</a>
<a href="/example3">ThisTextChanges</a>
<a href="/example4">ThisTextChanges</a>

I want to iterate through these and get the "ThisTextChanges" which are some numbers that changes, most accurately timers.

How can i achieve that? jquery is fine. They are inside a div with id "main_container". I need to put the text in a var so the href is importanto to know which var i use for each one.

2
  • Use .contains(myVar) in your selector for a tags. Commented Oct 5, 2017 at 3:26
  • do you mean if <a href="/example1">123</a> will produce var named example1 with value 123, and so on? Commented Oct 5, 2017 at 4:14

6 Answers 6

1

Lets break the task down into several steps:

  • Get a handle to all of our links (document.querySelectorAll)
  • learn how to get the current text of an a tag (childNode[0].nodeValue)

  • put it all together (Array.from, Array.map)

Get a handle to all of our links:

we will use document.querySelectorAll to get list of all nodes that match our selector. here I'm just going to use the selector a, but you probably have a class that specifies these links vs other links on the page:

var links = document.querySelectorAll('a');

Get the text of a link

This one is a bit more complicated. There are several ways to do this, but one of the more efficient ways is to loop through the child nodes (which will mostly be text nodes), and append the node.nodeValue for each one. We could probably get away with just using the nodeValue of the first child, but instead we'll build a function to loop through and append each.

function getText(link){
    var text = "";
    for (var i = 0; i < link.childNodes.length; i++){
        var n = link.childNodes[i];
        if (n && n.nodeValue){
            text += n.nodeValue;
        }
    }
    return text;   
} 

Put it all together

To put it all together we will use Array.map to turn each link in our list into the text inside it. This will leave us with an array of strings. However in order to be able to pass it to Array.map we will have to have an array, and document.querySelectorAll returns a NodeList instead. So to convert it over we will use Array.from to turn our NodeList into an array.

function getText(link){
    var text = "";
    for (var i = 0; i < link.childNodes.length; i++){
        var n = link.childNodes[i];
        if (n && n.nodeValue){
            text += n.nodeValue;
        }
    }
    return text;
}

var linkTexts = Array.from(document.querySelectorAll('a'))
                .map(getText);

console.log(linkTexts);
<a href="1">this is text</a>
<a href="2">this is some more text</a>

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

Comments

0

You can just add condition in the a selector as follows:

var array = [];
$('#main_container a[href="/example2"]').each(function(){
   array.push($(this).html());
});
console.log(array);

Comments

0

You can iterate and store them in an Array

var arr = [];   

$("a").each(function(){

  arr.push($(this).text());
  console.log( arr );

});

Comments

0

you can achieve that in may ways. this example using for loop.

var main_container = document.getElementById("main_container");
var items = main_container.getElementsByTagName("a");
for (var i = 0; i < items.length; ++i) {
      // do something.....
}

Comments

0

var array = [];
$('#main_container a').each(function(){
   array.push($(this).html());
});
console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main_container">
      <a href="/example1">ThisTextChanges 1</a>
      <a href="/example2">ThisTextChanges 2</a>
      <a href="/example3">ThisTextChanges 3</a>
      <a href="/example4">ThisTextChanges 4</a>
    </div>

5 Comments

This looks simple and that does it. How can i do it so it searches for a specific url ? for example only gather the text of '/example2' ? cause thats the only way i know wich parse will go into each var, it's by the url associated.
Glad i could help you, please accept if it solves your issue or help you!
I need a little more help, i was trying to break line and posted before i finished writing.
In that case you just need add condition in the anchor tag as follows: var array = []; $('#main_container a[href="/example2"]').each(function(){ array.push($(this).html()); }); console.log(array);
take a look at my answer at the bottom with the code snippet, i couldnt paste code in the comment section!
0

Please try:

$('#main_container > a[href]').each(function() {
    var tes = $(this).attr('href').substring(1);
    window[tes] = $(this).text();
});

<a href="/example1">123</a> will produce var named example1 with value 123, and so on.

Comments

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.