I am new to js, ​​please tell me how to solve this. I have the following structure

<a>link</a> <ul class="r"> <li> <div class="name">A789</div> <div class="id">7</div> </li> <li> <div class="name">B7</div> <div class="id">87</div> </li> </ul> 

It is necessary to get the name, id values ​​by clicking on the link and add them to the list. Example below

 window.dataR = window.dataR || []; dataR.push({ "eco": { "purchase": { "actionField": { "id" : 1 }, "products": [ { "id": "A789", "name" : "7", }, { "id": "B7", "name" : "87", } ] } } }); 

To do this, I write, but nothing comes out

  $(document).on("click", "a", function(){ $.each($("ul.r"), function(i,e)){ dataR.push.eco.products($(e).find("name")) dataR.push.eco.products($(e).find("id")) } }) 
  • "Example below" - an example of what? What is dataR ? To which list (array?) Should the data be added - to dataR or to dataR[0].eco.products ? Data should be collected from all ul on the page? Do they all contain elements of the same structure? - Igor
  • I need to add data from ul with class r to the dataR object products field - bot

1 answer 1

 var dataR = []; dataR.push({ "eco": { "purchase": { "actionField": { "id" : 1 }, "products": [ ] } } }); $(document).on("click", "a", function(){ $("ul.r li .id").each(function(index){ var _id = $(this).text(); var _name = $(this).closest("li").find(".name").text(); dataR[0].eco.purchase.products.push({ "id": _id, "name": _name }); }); console.log(dataR); return false; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a>link</a> <ul class="r"> <li> <div class="name">A789</div> <div class="id">7</div> </li> <li> <div class="name">B7</div> <div class="id">87</div> </li> </ul> 

  • thanks now I figured out - bot
  • @bot checkmark on the left - click :) - Igor