From 1758943d7ca3df5a25002108fc5a699086d1a4c9 Mon Sep 17 00:00:00 2001 From: Simon Babay Date: Mon, 1 Aug 2016 21:42:50 +0200 Subject: [PATCH] getValue() --- assets/scripts/src/choices.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index 5ace016..5dbcaa9 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -395,6 +395,31 @@ export class Choices { return this; } + + /** + * Get value(s) + * @param {Boolean} onlyValue Get only values of selected items, otherwise return selected items + * @return {Array/String} selected value (select-one) or array of selected items (inputs & select-multiple) + * @public + */ + getValue(onlyValue){ + const items = this.passedElement.type === 'text' ? this.store.getState().items : this.store.getState().choices; + let selectedItems = []; + + items.forEach((item) => { + if (this.passedElement.type === 'text'){ + selectedItems.push(onlyValue ? item.value : item); + }else if(item.selected) { + selectedItems.push(onlyValue ? item.value : item); + } + }); + + if (this.passedElement.type == 'select-one') { + return selectedItems[0]; + } else { + return selectedItems; + } + } /** * Select value of select box via the value of an existing choice