diff --git a/layouts/joomla/html/formbehavior/chosen.php b/layouts/joomla/html/formbehavior/chosen.php
index 71f14519e5407..3c35941512a49 100644
--- a/layouts/joomla/html/formbehavior/chosen.php
+++ b/layouts/joomla/html/formbehavior/chosen.php
@@ -28,11 +28,17 @@
// Options array to json options string
$options_str = json_encode($options, ($debug && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false));
-
JFactory::getDocument()->addScriptDeclaration(
- "
- jQuery(document).ready(function (){
- jQuery('" . $selector . "').chosen(" . $options_str . ");
- });
- "
-);
\ No newline at end of file
+ '
+ jQuery(function ($) {
+ initChosen();
+ $("body").on("subform-row-add", initChosen);
+
+ function initChosen(event, container)
+ {
+ container = container || document;
+ $(container).find(' . json_encode($selector) . ').chosen(' . $options_str . ');
+ }
+ });
+ '
+);
diff --git a/media/system/js/subform-repeatable-uncompressed.js b/media/system/js/subform-repeatable-uncompressed.js
index a778652b1c2fb..13eafb112a74b 100644
--- a/media/system/js/subform-repeatable-uncompressed.js
+++ b/media/system/js/subform-repeatable-uncompressed.js
@@ -226,11 +226,6 @@
// method for hack the scripts that can be related
// to the one of field that in given $row
$.subformRepeatable.prototype.fixScripts = function($row){
- // init chosen if any
- if($.fn.chosen){
- $row.find('select.advancedSelect').chosen();
- }
-
//color picker
$row.find('.minicolors').each(function() {
var $el = $(this);
diff --git a/media/system/js/subform-repeatable.js b/media/system/js/subform-repeatable.js
index a5f1ab8a7191f..78f1be6628a68 100644
--- a/media/system/js/subform-repeatable.js
+++ b/media/system/js/subform-repeatable.js
@@ -1 +1,2 @@
-(function(e){"use strict";e.subformRepeatable=function(t,n){this.$container=e(t);if(this.$container.data("subformRepeatable"))return r;this.$container.data("subformRepeatable",r),this.options=e.extend({},e.subformRepeatable.defaults,n),this.template="",this.prepareTemplate(),this.$containerRows=this.options.rowsContainer?this.$container.find(this.options.rowsContainer):this.$container,this.lastRowNum=this.$containerRows.find(this.options.repeatableElement).length;var r=this;this.$container.on("click",this.options.btAdd,function(t){t.preventDefault();var n=e(this).parents(r.options.repeatableElement);n.length||(n=null),r.addRow(n)}),this.$container.on("click",this.options.btRemove,function(t){t.preventDefault();var n=e(this).parents(r.options.repeatableElement);r.removeRow(n)}),this.options.btMove&&this.$containerRows.sortable({items:this.options.repeatableElement,handle:this.options.btMove,tolerance:"pointer"}),this.$container.trigger("subform-ready")},e.subformRepeatable.prototype.prepareTemplate=function(){if(this.options.rowTemplateSelector){var t=this.$container.find(this.options.rowTemplateSelector)[0]||{};this.template=e.trim(t.text||t.textContent)}else{var n=this.$container.find(this.options.repeatableElement).get(0),r=e(n).clone();try{this.clearScripts(r)}catch(i){window.console&&console.log(i)}this.template=r.prop("outerHTML")}},e.subformRepeatable.prototype.addRow=function(t){var n=this.$containerRows.find(this.options.repeatableElement).length;if(n>=this.options.maximum)return null;var r=e.parseHTML(this.template);t?e(t).after(r):this.$containerRows.append(r);var i=e(r);i.attr("data-new","true"),this.fixUniqueAttributes(i,n);try{this.fixScripts(i)}catch(s){window.console&&console.log(s)}return this.$container.trigger("subform-row-add",i),i},e.subformRepeatable.prototype.removeRow=function(e){var t=this.$containerRows.find(this.options.repeatableElement).length;if(t<=this.options.minimum)return;this.$container.trigger("subform-row-remove",e),e.remove()},e.subformRepeatable.prototype.fixUniqueAttributes=function(t,n){this.lastRowNum++;var r=t.attr("data-group"),i=t.attr("data-base-name"),n=n||0,s=Math.max(this.lastRowNum,n+1),o=i+s;this.lastRowNum=s,t.attr("data-group",o);var u=t.find("*[name]"),a={};for(var f=0,l=u.length;f=this.options.maximum)return null;var i=t.parseHTML(this.template);e?t(e).after(i):this.$containerRows.append(i);var a=t(i);a.attr("data-new","true"),this.fixUniqueAttributes(a,o);try{this.fixScripts(a)}catch(t){window.console&&console.log(t)}return this.$container.trigger("subform-row-add",a),a},t.subformRepeatable.prototype.removeRow=function(t){var e=this.$containerRows.find(this.options.repeatableElement).length;e<=this.options.minimum||(this.$container.trigger("subform-row-remove",t),t.remove())},t.subformRepeatable.prototype.fixUniqueAttributes=function(e,o){this.lastRowNum++;var i=e.attr("data-group"),a=e.attr("data-base-name"),o=o||0,r=Math.max(this.lastRowNum,o+1),n=a+r;this.lastRowNum=r,e.attr("data-group",n);for(var s=e.find("*[name]"),l={},p=0,f=s.length;passertContains(
- "jQuery('testSelect').chosen",
+ '$(container).find("testSelect").chosen',
$document->_script['text/javascript'],
'Verify that the Chosen JS is initialised with the supplied selector'
);