Is it necessary to use the second line here?
$("message",xml).each(function(id) {
message = $("message",xml).get(id);
msgID = $("msgID",message).text();
Isn't there some kind of 'this' keyword to eliminate the second line?
Thanks!
$('message', xml).each(function() {
var msgID = $(this).find('msgID').text();
}
Assuming this structure:
<root>
<message>
<msgID>123</msgID>
</message>
<message>
<msgID>234</msgID>
</message>
<message>
<msgID>345</msgID>
</message>
</root>
this keyword refers to the XML node object (or whatever it's name is), but find() is a jQuery method and it can only be called on jQuery objects.$(this) in a variable. Like var $this = $(this);...$(this). Performance-wise, it's not an issue. jsfiddle.net/aFUKHWhen you're in an .each(), this will represent the current item in the iteration.
The .each() also gives you 2 parameters. The first is the current index number of the iteration, and the second is the item in the iteration, same as this.
$("message",xml).each(function( idx, val ) {
// in here, "this" is the current "message" node in the iteration
// "i" is the current index in the iteration
// "val" is the same as "this"
});
When you do $("message",xml), you are looking for "message" nodes that are nested under the nodes at the top of xml. If any were found, the .each() will iterate over them.