How can I reduce this function? (Simple problem)

Asked

Viewed 22 times

0

Code:

<body>
 <div name = 'numeros' id = 'n1'>14</div>
 <div name = 'numeros' id = 'n2'>14</div>
 <div name = 'numeros' id = 'n3'>14</div>
</body>

<script>

 var numeros = document.getElementsByName('numeros')

 function reiniciar(){
   n1.innerHTML = ''
   n2.innerHTML = ''    //Dúvida
   n3.innerHTML = ''
 }
</script>

My goal is with one or two lines of code, exchange all values within 'numbers' by ' (all this within the restart Function)

1 answer

0


You can use the foreach in the number list:

var numeros = document.getElementsByName('numeros')
function reiniciar(){
    numeros.forEach(function(n){
          n.innerHTML = '';
     });  
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.