Reputation: 11
I'm trying to do the following: Each time you choose a radio button, these must become in stars. Each number must be the same number of stars.
I've only been able to make the first row works; as the picture shows.
I'm not using jquery. Only vanilla javascript (pure javascript )
const radio = document.getElementsByName("gender");
const boton = document.getElementById("btnBoton");
const listado = document.getElementById('contenedor-filas');
boton.addEventListener('click', enviaNumeros);
function enviaNumeros()
{
radio.forEach(function(elementos){
if(elementos.checked)
{
estrellas = document.createElement('h3');
estrellas.textContent = elementos.value;
listado.appendChild(estrellas);
console.log(elementos.value)
}
});
ponerEstrellas();
}
function ponerEstrellas()
{
let c = document.getElementsByTagName('h3');
for (i = 0; i < c.length; i++){
if (c[i].textContent === '5'){
document.querySelector('h3').innerText = "*****";
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Review System</title>
<!-- style sheet -->
<!-- Font Awesome Icon Library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="container-fluid">
<div class="row">
<input type="radio" name="gender" value="5" id="radio5"> 5<br>
<input type="radio" name="gender" value="4" id="radio4"> 4<br>
<input type="radio" name="gender" value="3" id="radio3"> 3<br>
<input type="radio" name="gender" value="2" id="radio2"> 2<br>
<input type="radio" name="gender" value="1" id="radio1"> 1<br>
<input type="submit" value="enviar" id="btnBoton">
</div>
</div>
<section class="lista-comentarios">
<div class="container-fluid" id="contenedor-filas">
<h1>Comment List</h1>
</div>
</section>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<script src="pruebas.js"></script>
</body>
</html>
I appreciate your help guys.
Upvotes: 0
Views: 56
Reputation: 500
document.querySelector
will only select the first h3 element in the document.
Upvotes: 0
Reputation: 590
You seem to be adding rows with the number value of the radio button as text, and then converting every row from numbers to '*'. Why not just convert to '*' before adding rows? You only need one function then.
const radio = document.getElementsByName("gender");
const boton = document.getElementById("btnBoton");
const listado = document.getElementById('contenedor-filas');
boton.addEventListener('click', enviaNumeros);
function enviaNumeros()
{
radio.forEach(function(elementos){
if(elementos.checked)
{
estrellas = document.createElement('h3');
let s = '';
for (let i = 0; i < elementos.value; i++) {
s += '*';
}
estrellas.textContent = s;
listado.appendChild(estrellas);
}
});
}
<div class="container-fluid">
<div class="row">
<input type="radio" name="gender" value="5" id="radio5"> 5<br>
<input type="radio" name="gender" value="4" id="radio4"> 4<br>
<input type="radio" name="gender" value="3" id="radio3"> 3<br>
<input type="radio" name="gender" value="2" id="radio2"> 2<br>
<input type="radio" name="gender" value="1" id="radio1"> 1<br>
<input type="submit" value="enviar" id="btnBoton">
</div>
</div>
<section class="lista-comentarios">
<div class="container-fluid" id="contenedor-filas">
<h1>Comment List</h1>
</div>
</section>
Upvotes: 1