Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting values from multiple select option box – Jquery

I am trying to select values in a multiple select option box. When i run my current code below, no item is selected in the option box but when i do parseInt(value) it only selects just the first option.

When i console.log(selected), it returns 4,5. How can insert 4,5 into $("#edit-items") to make it select both values in the option box.

PS: Sorry for my english

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

JS

 var selected = $(this).attr('data-items');  returns 4,5
 items = parseInt(selected)
 $("#edit-items").val([items]);

>Solution :

parseInt() will only parse the first integer from 4,5. If you want to get all the intgers as an array, split the string and call parseInt() on each array element.

items = selected.split(',').map(str => parseInt(str));
$("#edit-items").val(items);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading