1
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Protipo Software de Evento</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<p id="demo" style="font-family:courier"></p>
<form style="background-color: gray;" action="" method="post" id="formulario">
<input type="button" id="novoProd" value="Novo serviço" />
<input type="submit" value="Enviar" onclick="myReport()"/>
Valor total do evento é de R$
<p style="display:inline; color:yellow"id="result"></p>
<p class="demo"></p>
<div id="item" class="item">
<!-- Serviços -->
<label>Selecione o(s) serviço(s) para o seu evento:</label>
<select name="produtoId[]">
<option value="Buffet1">Buffet 1</option>
<option value="Decoração1">Decoração 1</option>
<option value="Transporte1">Transporte 1</option>
</select>
<!-- TOTAL de custos -->
<input type="number" class="qtd" onchange="myTotal()">
<!-- TOTAL de cada LINHA -->
<!-- <table style="display:inline"><th>R$</th></table> -->
<p style="display:inline" class='til'></p>
</div>
</form>
<!-- Calcula o TOTAL -->
<script>
function myTotal() {
var total = 0;
var total2 = 0;
var x = document.getElementsByClassName("qtd");
var i;
for (i = 0; i < x.length; i++) {
total = total + parseInt(x[i].value);
total2 = parseInt(x[i].value);
}
document.getElementById("result").innerHTML = total;
document.getElementsByClassName("til")[0].innerHTML = total2;
}
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#novoProd").click(function() {
var novoItem = $("#item").clone().removeAttr('id');
novoItem.children('input').val(''); //limpa o campo quantidade
$("#formulario").append(novoItem);
});
});
</script>
</body>
</html>
The script tries:
Bring the value of class "Qtd" to "tiled" by CLASS (Here it works)
How to follow the "Qtd" Class Array with the "til" CLASS"?
document.getElementsByClassName("til").innerHTMLThat won’t work because thegetElementsByClassNamereturns aHTMLCollectionwhich is kind of aArray– Icaro Martins
The code is all wrong, cloning elements with id. You cannot duplicate id’s
– Sam
Ready @Sam, no longer copying ID’s
– Sergio Bentes
Why do you use id and do a trick to remove the attribute in the script? rs...
– Sam
We only use id when an element is unique, and look at it like that. Even though the element is unique what I see around is using class.
– Sam
@Sam, use ID only to calculate the total that is visualized at the top, it is unique and does not repeat. Lines repeat, so I have to receive the array value of the input class "Qtd" and return to the side for the "til" class of the <p tag>
– Sergio Bentes
But it does not need id if you have a class in the same element. If you want to get the first element with the class just do
$(".item"), or$(".item:eq(0)"... For me to put id and then remove in the clone... don’t you need that, young man– Sam
Also doing a mix of pure JS with jQuery rs.. Since you paid 80KB to use jQuery, do everything in jQuery even. :)
– Sam
Missing also indent the code, get confused to understand. Good luck there!
– Sam
@Sam, I found;
<input type="number" class="qtd" id="on" onchange="myTotal()">I removed it because I was using it in other test functions. Current version is<input type="number" class="qtd" onchange="myTotal()">– Sergio Bentes
Do not change the title of the question by asking (solved)... if it has been solved you must mark an answer with , as you have already done. Everyone will know it’s been solved.
– Sam