0

I need to parse the following example XML in jquery, to get the attribute "V"

XML file:

<RES>
<R N="1">
    <MT N="myMeta1" V="myMeta1Value"/>
    <MT N="myMeta2" V="myMeta2Value"/>
    <MT N="myMeta2" V="myMeta2Value"/>
</R>
</RES>

And my javascript is the following:

function(data){
$(data).find('R').each(function(){
    var $result = $(this);
    $result.find('MT').each(function(_mt) {
            console.log($(_mt).attr("V") );
    });
});

}

I get undefineds, what am I doing wrong?

2 Answers 2

7

The first argument to .each callback is the index, the second one is the value. You can also use this:

$result.find('MT').each(function() {
        console.log($(this).attr("V") );
});

Or:

$result.find('MT').each(function( index, _mt ) {
        console.log($(_mt).attr("V") );
});
Sign up to request clarification or add additional context in comments.

Comments

2

You are using index as an element in each. As first parameter is index pass two parameter in each and use the second to get the element.

function(data){
  $(data).find('R').each(function(){
      var $result = $(this);
      $result.find('MT').each(function(_mt, obj) {
            console.log($(obj).attr("V") );
      });
   });
}

Comments

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.