2

I have a list of order and if element is selected (checked) - it sums the value and shows the text of element that is selected. How to show text of selected elements and remove it when unchecked? Link to code pen.

And the js itself:

$(".price-of-package-element").change(function () {
    var count = 0,
        priceElement = $(".price-of-package-element");   
  for (var i = 0; priceElement[i]; ++i) {
        if (priceElement[i].checked) {
            var value = priceElement[i].value,
                title = $(this).next().text();
            count += Number(priceElement[i].value);
            $('#text').append("<div>" + title + "</div>");

        }
    } 
});
1
  • Look at my edit please Commented Feb 27, 2016 at 12:34

2 Answers 2

1

Just add $('#text').html(''); before append new one because you appending all div again when new checkbox is checked.

Use following code.

$(".price-of-package-element").change(function () {
  var count = 0,
  priceElement = $(".price-of-package-element");   
  $('#text').html('');
  for (var i = 0; priceElement[i]; ++i) {
        if (priceElement[i].checked) {
            var value = priceElement[i].value,
                title = $(this).next().text();
            count += Number(priceElement[i].value);
            $('#text').append("<div>" + title + "</div>");

        }
    } 
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah. it removes appended... but see the result if any other tab is select (to the left).
0

The code given by @VivekPipaliya replace all the previously texts by the last appended.

The following code will works :

$(".price-of-package-element").change(function () {
  var count = 0,
  priceElement = $(".price-of-package-element");   
  for (var i = 0; priceElement[i]; ++i) {
        if (priceElement[i].checked) {
            var value = priceElement[i].value,
                title = $(this).next().text();
            count += Number(priceElement[i].value);
        }
    } 
    $('#text').append("<div>" + title + "</div>"); // Append the div from out of the loop
});

Codepen

Update

I finally got it:

$(".price-of-package-element").change(function () {
    var priceElement = $(this);
    var id = priceElement.attr('id');
    var title = $(this).next().text();
    var textId = 'text-' + id;

    if ($(this).is(':checked')) {
        $('#text').append("<div id='"+ textId +"'>" + title + "</div>"); // Append the div from out of the loop
    } else {
        $('#'+textId).remove();
    }
});

CodePen

3 Comments

Run your code in codepen.io/NeedHate/pen/xVxgYM & then post the answer X-(
Yes, thanks I will add it. but yours squash appended elements by replace their text by the last given.
@NeedHate please re-try :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.