Iterate array in input element

Asked

Viewed 27 times

1

I need to generate a radio element according to the selection of a field. I’m having difficulties generating the block:

need to fill Radius value in that block according to the array setting below:

var horario = ['quinta/10:00:00', 'quinta/21:00:00','sexta/10:00:00','domingo/15:00:00']; 

 <div id="valores">
    <input class="sr-only" name="dias" type="checkbox" value="" ><span class="checkbox-label">
    </span></input>
</div>

1 answer

1


You can do this by creating inputs with HTML strings in Javascript:

var horario = ['quinta/10:00:00', 'quinta/21:00:00', 'sexta/10:00:00', 'domingo/15:00:00'];

var inputs = horario.map(function(h) {
  return [
    '<input class="sr-only" name="dias" type="checkbox" value="', h, '" />',
    '<span class="checkbox-label">', h, '</span>'
  ].join('');
}).join('<br>');

document.getElementById('valores').innerHTML = inputs;
<div id="valores">

</div>

Browser other questions tagged

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