0

How to get all href attributes values of

<ul>
   <li class="demo">
        <a href="/chat/2"></a>
   </li>
   <li class="demo">
        <a href="/chat/3"></a>
   </li>
</ul>

How can I get Href attributes values in one array?

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Vardan
  • 143
  • 1
  • 1
  • 6
  • Looks like a dupe of several other questions: https://stackoverflow.com/questions/7759823/jquery-get-the-href-attribute-for-each-link-in-a-section-of-html https://stackoverflow.com/questions/471606/get-all-hrefs-as-an-array-in-jquery And several others. – kmoser Oct 15 '17 at 19:40
  • Note that [this answer](https://stackoverflow.com/a/14359001/519413) in the marked duplicate is the best one as it uses `map()`, not the one with the highest votes. – Rory McCrossan Oct 15 '17 at 20:07

2 Answers2

0

var Arr=[];
$("li.demo").each(function(){
 Arr.push($(this).find("a").attr("href")); 
});
console.log(Arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
   <li class="demo">
        <a href="/chat/2"></a>
   </li>
   <li class="demo">
        <a href="/chat/3"></a>
   </li>
</ul>
Farhad Bagherlo
  • 6,725
  • 3
  • 25
  • 47
-1

let foo = document.getElementsByTagName("a");
console.log(foo)
let my_array = [];

for(let i=0;i<foo.length;i++) {
  my_array.push(foo.item(i).href);
}

console.log(my_array)
<ul>
   <li class="demo">
        <a href="/chat/2"></a>
   </li>
   <li class="demo">
        <a href="/chat/3"></a>
   </li>
</ul>
Steven Black
  • 1,988
  • 1
  • 15
  • 25