I'm trying to build a little form, with multiple select fields. My goal is to let user multi-select some options from a first multiple select field. I then display his choice, with, in front of each result, a new select field. This is working fine.
However what I don't know is how to get the user's first and second choices together by clicking on the second button (#confirmation)
jQuery(document).ready(function($) {
$("#yourChoice").on('click', function() {
$("#multiple option:selected").each(function() {
var html = '';
html += '<p><span class="yourchoice">' + $(this).text() + '</span>';
html += '<select id="quantity" name="quantity">';
html += ' <option value="10">10</option> ';
html += ' <option value="20">20</option> ';
html += ' <option value="30">30</option> ';
html += '</select></p>';
$('#choice').append(html);
})
})
$("#confirmation").on('click', function() {
var group = $("#multiple option:selected").each();
$("#quantity option:selected").each(function() {
var finalText = '';
finalText += '<span>' + $("#multiple option:selected").each().text() + '</span>';
finalText += '<span class="yourchoice">' + $(this).val() + '</span>';
$('#finalChoice').append(finalText);
})
})
})
.chosen-select {
width: 300px;
}
#quantity {
width: 300px;
margin-left: 20px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
<select id="multiple" name="multiple" multiple="multiple" class="chosen-select">
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
<option value="value3">Value 3</option>
<option value="value4">Value 4</option>
<option value="value5">Value 5</option>
<option value="value6">Value 6</option>
</select>
</p>
<button id="yourChoice">Add your choices</button>
<p>Choose a quantity</p>
<div id="choice"></div>
<button id="confirmation">Resume</button>
<div id="finalChoice"></div>
idon multiple elements which is invalid, and partly because you useeach()with no handler function in a couple of places which is causing errors. What exactly should the output in#finalChoicelook like after clicking#confirmation?