Search code examples
javascriptjqueryhtmlhref

javascript parse text from <a href> links


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.


Solution

  • 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);