Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change value #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ This example will create a Flagstrap Dropdown giving the input field the name of
</script>
```

##### 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.

Passing a string value to the plugin instead of an object will change value of the underlying select and update the UI display. The string passed should be the country code of the country to select.

The example below will take the flagstrap widget created in the example above and change its selected value to the United States.

```html
<script>
$('#flagstrap3').flagStrap('US');
</script>
```

### Options
<table class="table table-bordered table-striped">
<thead>
Expand Down
29 changes: 27 additions & 2 deletions src/jquery.flagstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,26 @@

};

/**
* 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
var html = '';
if ( country_code == plugin.settings.placeholder.value ) {
html = '<i class="flagstrap-icon flagstrap-placeholder"></i> ' + plugin.settings.placeholder.text;
} else {
html = $container.find('li a[data-val='+country_code+']').html();
}
$('.flagstrap-selected-' + uniqueId).html( html );
}

var buildHtmlSelect = function () {
var htmlSelectElement = $('<select/>').attr('id', htmlSelectId).attr('name', plugin.settings.inputName);

Expand Down Expand Up @@ -449,8 +469,13 @@
$.fn.flagStrap = function (options) {

return this.each(function (i) {
if ($(this).data('flagStrap') === undefined) {
$(this).data('flagStrap', new $.flagStrap(this, options, i));

if ( typeof options === 'string' && $(this).data('flagStrap') !== undefined ) {
$(this).data('flagStrap').val(options);
} else {
if ($(this).data('flagStrap') === undefined) {
$(this).data('flagStrap', new $.flagStrap(this, options, i));
}
}
});

Expand Down