James Korden
James Korden

Reputation: 634

How to use jQuery to get data inside div's

I have the following:

jQuery('.activity-feed .comments div > li').each(function() {
 // Refer to each piece of data here
});

The problem I am having, is that for each of these div's, I want to pull some data from a child of a child of a child etc etc. How do I reference these DOM elements? I tried:

jQuery(this.find(".comment-container .note .activity-event div .meta a").text());

But this didn't work. I just need to strip data out into variables. I'm sure it's simple but I haven't done anything similar before and nothing I search for quite covers it.


Please note, I am aware $() would be nicer than jQuery() but it won't work with the system I am manipulating.

Upvotes: 3

Views: 3699

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

this in the each iteration will refer to the DOM element. If you want to use jQuery methods on that element, you need to wrap it in a jQuery object: $(this):

jQuery(this).find(".comment-container .note .activity-event div .meta a").text();

Please note, I am aware $() would be nicer than jQuery() but it won't work with the system I am manipulating.

A jQuery object is passed in to the DOMReady handler by default, so you can still use the $ variable within the scope of that handler only. This should not interfere with any other scripts you have. Try this:

jQuery(function($) {
    $('.activity-feed .comments div > li').each(function() { 
        var foo = $(this).find(".comment-container .note .activity-event div .meta a").text();
    });
});

Upvotes: 6

Related Questions