Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pure JavaScript
- function selectToCollection(selector, asJson) {
- var options = Array.from(document.querySelector(selector).querySelectorAll('option'));
- var collection = options.reduce((prev, curr) => {
- return [...prev, { label: curr.label, value: curr.value}];
- }, []);
- return (asJson === true) ? JSON.stringify(collection) : collection;
- }
- // With jQuery
- function selectToCollection(selector, asJson) {
- var collection = $(selector).find('option').toArray().reduce((prev, curr) => {
- return [...prev, { label: curr.label, value: curr.value}];
- }, []);
- return (asJson === true) ? JSON.stringify(collection) : collection;
- }
Add Comment
Please, Sign In to add comment