skip to Main Content

I want to get every link from my html code to load on click a div class content from an other page:
I just can’t get it right but I already got the first link…
This is my function:


    $("a.button.view").click(function(e) {
        links = $("a.button.view").attr('href');
        e.preventDefault()

        $(".wrapper-error").load(links + " .wrapper-order-details");

    });
```
this is my html:
```
<a href="http://localhost:8080/account/view-order/426/" class="button view">View</a>
<a href="http://localhost:8080/account/view-order/427/" class="button view">View</a>
<a href="http://localhost:8080/account/view-order/428/" class="button view">View</a>
```

2

Answers


  1. you can use jquery to achieve this

    var links = [];
    $('a').each(function() {
       links.push( this.href ); 
    });
    console.log(links); 
    
    Login or Signup to reply.
  2. Using map() is shortest approach

    const links = $('a').map((_,el) => el.href).get()
    
    console.log(links)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <a href="http://localhost:8080/account/view-order/426/" class="button view">View</a>
    <a href="http://localhost:8080/account/view-order/427/" class="button view">View</a>
    <a href="http://localhost:8080/account/view-order/428/" class="button view">View</a>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search