0

I would like to display dynamically a list of the chosen options using javascript or jQuery when the user changes his options. PS: I'm using Django + Python to generate the options and bootstrap to style them.

Here is my code:

{% for option in options %}
    <div class="input-group" style="margin-bottom: 7px;">
        <div class="input-group-prepend">
            <div class="input-group-text">
                <input type="checkbox" name="checks[]" aria-label="Radio button for following text input" class="option_choice" class="ipad_pro" value="{{ option.name }}">
            </div>
        </div>
        <input style="background-color: #fff;" type="text" class="form-control" disabled=True value="{{ option.name }} {{ option.price }}€" aria-label="Text input with radio button">
    </div>
{% endfor %}
<ul id='options_display'></ul>

Here's what I've attempted:

var getChecked= function() {
    var n = $("input:checked");
    var options_display = $("#options_list")
    console.log(n)
    if (n.length != 0) {
            $.each(n, function(index, value) {
            options_display.append("<li>" + value.defaultValue + "</li>")
        });
    }
    else {
        options_display.html("<h3 class'text-center'>No Options</h3>")
    }
};
getChecked()
$( "input[type=checkbox]" ).on( "click", getChecked);

The problem with that is that it doesn't reset the content of the <ul> which means that I get elements twice when I select a new one. Please help me if you know the answer to my problem.

3
  • There is no countChecked in your code!!!! Commented Jul 28, 2018 at 7:45
  • I just wrote changed COuntChecked to get getChecked. Commented Jul 28, 2018 at 7:45
  • Ths problem is not here Commented Jul 28, 2018 at 7:45

1 Answer 1

1

You can empty options_display on each click like

$("#options_list").empty();

before the condition.

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

1 Comment

@FantasmoClone27, you are most welcome. Glad to help :)

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.