javascriptjqueryeach

count number up with XML Elements


I build my XML with my base XML like this:

$(xml).find('PARENT').find('CHILDREN').each(function(i){
  outputstr += '<lorem id="">'
})

now the Object "parent" exists a few times in every XML I read, I want to count up the ID Attribute to every lorem build.

then my output XML should look like this:

<lorem id="1"/>
<lorem id="2"/>
<lorem id="3"/>

how do I count up each time a Element was build


Solution

  • You can use a template literal to substitute i into the output.

    $(xml).find('PARENT').find('CHILDREN').each(function(i) {
      outputstr += `<lorem id="${i+1}">`
    })