From 6e0143104c49ef4f52f0a435aa32b673312df7cf Mon Sep 17 00:00:00 2001 From: Git-Admin Date: Fri, 14 Sep 2018 12:40:40 +0200 Subject: [PATCH 1/3] Added ability to change selection after initialization --- README.md | 13 +++++++++++++ src/jquery.flagstrap.js | 23 +++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ad727f1..df424ab 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,19 @@ This example will create a Flagstrap Dropdown giving the input field the name of ``` +##### Setting value of widget after creation +You cannot set the value of the widget using the val() method on the underlying select, as this does not notify the plugin of the change. + +In order change the underlying value of the select and the UI display of the flag widget by passing a string in options instead of an object. The string passed should be the country code of the country to select. + +The example below will take an already setup flagStrap widget, and change its selected value to the United States. + +```html + +``` + ### Options diff --git a/src/jquery.flagstrap.js b/src/jquery.flagstrap.js index 77a0212..98ce777 100644 --- a/src/jquery.flagstrap.js +++ b/src/jquery.flagstrap.js @@ -318,6 +318,20 @@ }; + /** + * Acts like the jQuery val() but on the actual flagStrap object not the underlying + * select. For this reason it also does not fire the change event, as in jQuery val() does + * not cause triggering over underlying change events + * + * @param {string} country_code + */ + plugin.val = function(country_code) { + // set the underlying select to the new value + $(htmlSelect).val(country_code); + // update the UI of the flag widget to show the new country selection + $('.flagstrap-selected-' + uniqueId).html( $container.find('li a[data-val='+country_code+']').html() ); + } + var buildHtmlSelect = function () { var htmlSelectElement = $('