0

I have an webpage and I want to get all the links in an arrayset. My page has elements stacked up something like this:

<ul class='abc'
<li>
<div>
<h3> <a href="www.1.com"> hey1</a>
     <span class="xyz">  heyxyz </span>
</h3>
</div>
</li>
<ul>

Similarly I have 17 UL elements with the same class that goes on like this.

My aim is to capture

  • www.1.com
  • Hey 1
  • heyxyz

in an array.
When i do an

$('.abc')
in google developer tools. I get all my 17 **<ul class="abc">** elements.

This is as far as i could go (i am very new to Jquery.)

Please help me dig this process out?

1 Answer 1

2

You can use the map method and create an array of objects:

var arr = $('.abc').map(function () {
    var $this = $(this);
    return {
        'a_href':    $this.find('a').attr('href'),
        'a_text':    $this.find('a').text(),
        'span_text': $this.find('span.xyz').text()
    };    
}).get();
Sign up to request clarification or add additional context in comments.

2 Comments

I don't seem to understand this. I just need to be able to parse after i get in the elements . Is there any other way to get it into an array?(i would require 3 such arrays).
@BoyLittUp As I said this creates an array of objects. One object for each ul. You can also use the jQuery .each() method and .push() the desired values into an array (if this makes sense).

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.