Display elements one by one with jquery .each

1

Hello everyone. I'm having problems with jQuery .each. I have an xml like this:

<dados>
<usuarios>
    <usuario id="1">
        <login>usuario12</login>
        <senha>21</senha>
    </usuario>
    <usuario id="2">
        <login>usuario23</login>
        <senha>23</senha>
    </usuario>
</usuarios>

And I'm trying to read it like this with jQuery:

$(xml).find("usuarios").each(function() {
        console.log($(this).find("usuario").text());
});

but the console shows me this way:

  

user1212user2323

Can you help me?

    
asked by anonymous 07.06.2017 / 18:56

1 answer

2

You gave .find by usuarios , but you only have 1 usuarios . Try to only put usuario , like this:

var xml = '<dados><usuarios><usuario id="1"><login>usuario12</login><senha>21</senha></usuario><usuario id="2"><login>usuario23</login>        <senha>23</senha></usuario></usuarios>';

$(xml).find('usuario').each(function () {
  console.log("id:" + $(this).attr('id'));
  console.log("login:" + $(this).find('login').text());
  console.log("senha:" + $(this).find('senha').text());

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
07.06.2017 / 18:59