-1

I have this code that came from this link for experiment purposes

jQuery: What's the difference between '$(this)' and 'this'?

$("#orderedlist").find("li").each(function(i) {
  $(this).append(" BAM! " + i);
});

the code $(this).append(" BAM! " + i);

How do you call the child element of this? for example

$(this + ".className").append(" BAM! " + i);

Something like that, but it doesn't work. Any synthax that works for this kind of stuff?

Community
  • 1
  • 1
Wesley Brian Lachenal
  • 4,381
  • 9
  • 48
  • 81

2 Answers2

2

With method .find() you can search inside the object all child levels. try this;

$(this).find(".className").append(" BAM! " + i);

If you would like to search only into the first child level you can use .children() like this

$(this).children(".className").append(" BAM! " + i);
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
0

In your code example. You are iterating through each <li> found in #orderedList and you are appending the text BAM! [digit] so $(this) is only referring to the <li>

If you did .append('<span> BAM! ' + i + '</span>') then you can find the child span like this:

var child_text = $(this).append('<span> BAM! ' + i + '</span>').find('span').text();
alert(child_text);
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77