The goal is to build an estimator interface for my customer.
So I've several boxes with items the customer can add or remove (domain name, hosting, ...).
The result of the selected items is shown here:
<ul class="calculator__sidebar-ul">
</ul>
The problem with my code is each time a user click on an item, it adds a new line whereas it should replace the one in place.
How can I change this behavior ?
$('input[name="hosting"]').click(function() {
value = $(this).data('value');
title = $(this).data('title');
if($('li #hosting').length == 1) { $('li #hosting').remove(); }
$('.calculator__sidebar-ul').append('<li id="hosting">'+ title +'<span class="calculator__sidebar-price"> + '+ value +' $</span></li>');
});
$('input[name="domain"]').click(function() {
value = $(this).data('value');
title = $(this).data('title');
if($('li #domain').length == 1) { $('li #domain').remove(); }
$('.calculator__sidebar-ul').append('<li id="domain">'+ title +'<span class="calculator__sidebar-price"> + '+ value +' $</span></li>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h4>Hosting (1 year)</h4>
<div class="form__form-group">
<label class="radio-btn calculator__radio-btn">
<input class="radio-btn__radio" type="radio" data-value="0" data-title="No hosting" name="hosting">
<span class="radio-btn__radio-custom"></span>
<span class="radio-btn__label">No hosting
<span class="calculator__color_label">+ 0$</span>
</span>
</label>
<label class="radio-btn calculator__radio-btn">
<input class="radio-btn__radio" type="radio" data-value="200" data-title="Yes, add one hosting" name="hosting">
<span class="radio-btn__radio-custom"></span>
<span class="radio-btn__label">Yes, add one hosting
<span class="calculator__color_label">+ 200$</span>
</span>
</label>
</div>
<h4>Domain name (1 year)</h4>
<div class="form__form-group">
<label class="radio-btn calculator__radio-btn">
<input class="radio-btn__radio" type="radio" data-value="0" data-title="No domain" name="domain">
<span class="radio-btn__radio-custom"></span>
<span class="radio-btn__label">No domain
<span class="calculator__color_label">+ 0$</span>
</span>
</label>
<label class="radio-btn calculator__radio-btn">
<input class="radio-btn__radio" type="radio" data-value="50" data-title="Yes, add one domain" name="domain">
<span class="radio-btn__radio-custom"></span>
<span class="radio-btn__label">Yes, add one domain
<span class="calculator__color_label">+ 50$</span>
</span>
</label>
</div>
<ul class="calculator__sidebar-ul">
</ul>
Thanks.
liis#hosting, solengthis always0. Once you change tohtml()you can remove theifstatement completelyif($('li #hosting').length == 1) {}is if the user change the choice of the radio.html()will overwrite any previous content, so the prior selections have no relevance to the current content