huggie
huggie

Reputation: 18247

JQuery grabbing stuff from nested tags

I'm sure this question must be answered somewhere, but well.

I got nested data I need to fetch from an HTML page. Imagine nested <ul>, <li>, <div> and so on.

And I need to turn them into JSON, so I'll build out the data in hierarchical Javascript objects first.

Since they are nested I would like to select the largest group, and build from there.

After I select the largest group, is there a way to further using a JQuery selector to select tag from within this larger group?

$('.someclass').each(function(i) {
  $(this); // what to do with this? so I select further nested data?
});

Upvotes: 0

Views: 27

Answers (1)

Remo H. Jansen
Remo H. Jansen

Reputation: 25029

Is this what you are looking for?

$('#myUl li').each(function(index, item) { // you can name the item here or use "this"

  var $li = $(this);
  // an alternative way is to do:
  var $li = $(item);

  // you can select items inside the li item, for example select an anchor
  var $a = $li.find("a");
  $a.attr("href","#home");
});

Upvotes: 1

Related Questions