I have a problem, I have a list and when the user clicks it adds the id of this <li>
to an array, but I want it when I click again on that <li>
, that id is removed from the id.
Here's the JSFIDDLE .
HTML:
<ul>
<li id="1">Teste 1</li>
<li id="2">Teste 2</li>
<li id="3">Teste 3</li>
<li id="4">Teste 4</li>
<li id="5">Teste 5</li>
<li id="6">Teste 6</li>
<li id="7">Teste 7</li>
<li id="8">Teste 8</li>
<li id="9">Teste 9</li>
<li id="10">Teste 10</li>
</ul>
<a href="#" id="show">Exibir selecionados</a>
<div id="retorno"></div>
JavaScript
$(function(){
var idsSelecionados = [];
$('li').click(function(){
var id = $(this).attr('id');
idsSelecionados.push(id+',');
$(this).toggleClass("active");
});
$('#show').on('click', function(){
$.each(idsSelecionados, function(i, val){
$('#retorno').append(val);
});
});
});