I can not get the value of a parent node in javascript

1

good afternoon programmers I have a very big question and I hope you can help me I have a function called usu which takes values from a div to be exact I take them with the valiable request the joke esque if I can get a child but I can not get the value of the father but the son consulted on the basis of the father

function usu(){

  var persona=document.getElementById("persona").innerHTML;
  var lista=document.getElementById("lista");
  var pedido=document.getElementById("lista-pedido");
  var radioid;
  var cantid=[];
  var produc=[];

  if(pedido.childNodes.length>1){
    var tamaño2=pedido.childNodes.length;
    for(var i=1; i<tamaño2; i++){
        var mesa2=pedido.childNodes[i];             
        cantid.push(document.getElementById(mesa2.id+"-1").innerHTML);
    }        
  }

  if(pedido.childNodes.length>1){
    var tamaño=pedido.childNodes.length;
    for(var i=1; i<tamaño; i++){
      var comida=pedido.childNodes[i];
      produc.push(comida.value);
    }
  }

  alert(cantid);
  console.log(cantid);
  console.log(produc);     
}

The correct thing would be to do this but it does not work either, it detects me to produce as an undefined

 if(pedido.childNodes.length>1){
    var tamaño2=pedido.childNodes.length;
    for(var i=1; i<tamaño2; i++){
        var mesa2=pedido.childNodes[i];
         produc.push(mesa.value);
         cantid.push(document.getElementById(mesa2.id+"-1").innerHTML);
    }

 }

I would appreciate your help to get the value of the div with the value of 227 thanks

    
asked by costadark1212 03.08.2018 в 00:35
source

1 answer

0

Try it with querySelectorAll() like this:

var pedido=document.querySelectorAll("div[id^=pedido]");    
var produc=[];
    
if(pedido.length>1){
    var tamaño2=pedido.length;
    for(var i=0; i<tamaño2; i++){
        var mesa2=pedido[i];            
        produc.push(mesa2.getAttribute("value"));
    }        
}
console.log(produc);
<div id="pedido277" value="277">
    <div id="pedido277-1" value="1"></div>
</div>
<div id="pedido278" value="278">
    <div id="pedido277-1" value="1"></div>
</div>
    
answered by 03.08.2018 / 01:22
source