2

I'm building a simple shopping cart where visitors can select a few items they want, click on the "Next" button, and see the confirmation list of things they just selected. I would like to have the confirmation list shown on each line for each item selected.

HTML selection

<div id="c_b">
  <input type="checkbox" value="razor brand new razor that everyone loves, price at $.99" checked> 
  <input type="checkbox" value="soap used soap for a nice public shower, good for your homies, price at $.99" checked>
  <input type="checkbox" value="manpacks ultimate choice, all in 1, price at $99">
</div>  

<button type='button' id='confirm'>Next</button>

HTML confirmation list

<div id='confirmation_list' style='display:none;'>
<h2>You have selected item 1</h2>
<h2>Your have selected item 2 </h2>
</div>

JS

$(function(){
   $('#confirm').click(function(){
    var val = [];
    $(':checkbox:checked').each(function(i){
      val[i] = $(this).val();
    });
  });
});

I ultimately want to replace the words 'Your have selected item 2' in h2s with the values selected from each check box. With the code above I'm able to collect the values of each checkbox into an array val, but having difficulty looping through and displaying them.

Any advice would be appreciated.

2 Answers 2

2

Try

$(function () {
    $('#confirm').click(function () {
        var val = [];
        var els = $('input:checkbox:checked').map(function (i) {
            return $('<h2 />', {
                text: 'You have selected item \'' + this.value + '\''
            })
        }).get();

        $('#confirmation_list').empty().append(els).show()
    });
});

Demo: Fiddle

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

2 Comments

Thanks a ton. What's the '\' after 'You have selected item' for?
@user3192948 just to add the selected items value with a pair of ''
1

Try this

<div id="c_b">
    <input type="checkbox" value="razor brand new razor that everyone loves, price at $.99" checked>
    <input type="checkbox" value="soap used soap for a nice public shower, good for your homies, price at $.99" checked>
    <input type="checkbox" value="manpacks ultimate choice, all in 1, price at $99">
</div>
<button type='button' id='confirm'>Next</button>
<div id='confirmation_list' style='display:none;'>
</div>

Your JS code should be

$(function () {
    $('#confirm').click(function () {
        $("#confirmation_list").empty();
        $('input:checkbox:checked').each(function (i) {
            $("#confirmation_list").append("<h2>You have selected item '" + $(this).val() + "'</h2>");

        });
        $("#confirmation_list").show();
    });
});

Fiddle

1 Comment

Thanks a ton. Worked like a charm.

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.