Mister98 Mister98 - 1 year ago 89
jQuery Question

Insert a multiple choice in a html page

What I have to do is to make the user select some languages among those present on a page and get an array or a string that contains all the selected languages.
Do you know how can I do?
The string or array it will serve subsequently in a JavaScript method.

Answer Source

you could use some checkboxes and save the values in an array like this:

var lang = [];

$('.cbLang').on('change', function() {
  this.checked ? lang.push(this.value) : removeLang(this.value);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input class="cbLang" type="checkbox" value="English">English
<input class="cbLang" type="checkbox" value="German">German
<input class="cbLang" type="checkbox" value="French">French

EDIT: If you also need a way to remove it from the array, check the JQuery grep(): http://api.jquery.com/jquery.grep/


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download