First step: change your script to somehow mark the selected item. I advise applying a class over the selected item.
.conteudo li.sel a {
color: #4682B4;
background: #fff;
border: 1px solid #4682B4
}
var linhas = $('.conteudo li');
$('a', linhas).click(function() {
var self = $(this);
var linha = self.parent();
linha.toggleClass("sel");
self.text(linha.hasClass("sel") ? "Remover" : "Selecionar");
});
once you are selected, just add them up.:
var linhas = $('.conteudo li');
var total = 0;
linhas.filter(".sel").each(function () {
var elem = $('p', this);
total += parseFloat(elem.text().replace(".", "").replace(",", "."));
});
the last step is to assign the value to the input.:
var intl = Intl.NumberFormat("pt-BR", { style: 'currency', currency: 'BRL' })
var dado = $('#dado');
dado.val(intl.format(total));
Follow the full example.:
$(document).ready(function() {
filtraServico($('#listaServicos li:eq(0)').attr('class'));
// CODIGO COMEÇA AQUI //
var intl = Intl.NumberFormat("pt-BR", { style: 'currency', currency: 'BRL' })
var dado = $('#dado');
var linhas = $('.conteudo li');
$('a', linhas).click(function() {
var self = $(this);
var linha = self.parent();
linha.toggleClass("sel");
self.text(linha.hasClass("sel") ? "Remover" : "Selecionar");
var total = 0;
linhas.filter(".sel").each(function () {
var elem = $('p', this);
total += parseFloat(elem.text().replace(".", "").replace(",", "."));
});
dado.val(intl.format(total));
});
});
function filtraServico(classe){
$('#listaServicos li').hide();
$('#listaServicos li.' + classe).show();
return false;
}
body {
font-family: Arial;
margin: 0;
}
a, a:hover, a:active { text-decoration: none; color: #fff; }
.opcoes {
list-style: none;
margin: 0; padding: 0 40px;
background: #4682B4;
color: #FFFFFF;
}
.opcoes li {
display: inline-block;
padding: 10px 15px;
}
.opcoes li.sel {
border-bottom: 4px solid #1d324a;
}
.conteudo {
background: #ececec;
}
.conteudo ul {
margin: 0; padding: 0;
list-style: none;
}
.conteudo ul li {
padding: 10px 40px;
}
.conteudo ul li:nth-child(2n+0) {
background: #fff;
}
.conteudo p { margin:0; padding:0; }
.conteudo li a {
color: #fff;
float: right;
margin: -24px 0 0 0; padding: 5px 8px;
background: #4682B4;
border-radius: 3px;
box-sizing: border-box;
text-align: center;
width: 100px;
}
.conteudo li.sel a {
color: #4682B4;
background: #fff;
border: 1px solid #4682B4
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="opcoes">
<a href="#" onclick="return filtraServico('op01');"><li class="sel">Opção 01</li></a>
<a href="#" onclick="return filtraServico('op02');"><li>Opção 02</li></a>
<a href="#" onclick="return filtraServico('op03');"><li>Opção 03</li></a>
</ul>
<div class="conteudo">
<ul id="listaServicos">
<li class="op01" id="selecionado">
<p class="insert">677,81</p>
<a href="#" >Selecionar</a>
</li>
<li class="op02" id="selecionado">
<p class="insert">569,81</p>
<a href="#" >Selecionar</a>
</li>
<li class="op02">
<p class="insert">642,71</p>
<a href="#" >Selecionar</a>
</li>
<li class="op03" id="selecionado">
<p class="insert">677,81</p>
<a href="#" >Selecionar</a>
</li>
</ul>
</div>
<hr />
<p>Valor total será colocado aqui:</p>
<input type="text" name="dado" id="dado" />