Bonjour, j'utilise sur un site un plugin jquery me permettant de transformer un simple select html en multiselect (même comportement que le select mais je peux sélectionner plusieurs valeurs dans la liste déroulante.

Il a fallut que j'intègre un mécanisme permettant a l'utilisateur d'ajouter un item dans la liste de choix du multiselect. La fonction que j'ai créé est extérieur au code du plugin.
Mon problème est que si j'arrive bien a injecter le nouvel item dans la liste, je n'arrive pas a forcer la mise a jour de la valeur affiché dans le multiselect. (auto selection du nouvel item)
Ci après le code du plugin, et en bas la fonction "addItemMultiSelect" qui ajoute l'item au multiselect.

Pourriez vous m'aider à résoudre mon problème d'auto selection et affichage de la nouvelle valeur ajouté. Merci.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
// jQuery multiSelect
//
// Version 1.0.1 beta
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 06 April 2008
//
// Visit http://abeautifulsite.net/notebook.php?article=62 for more information
//
// Usage: $('#control_id').multiSelect( options, callback )
//
// Options:  selectAll          - whether or not to display the Select All option; true/false, default = true
//           selectAllText      - text to display for selecting/unselecting all options simultaneously
//           noneSelected       - text to display when there are no selected items in the list
//           oneOrMoreSelected  - text to display when there are one or more selected items in the list
//                                (note: you can use % as a placeholder for the number of items selected).
//                                Use * to show a comma separated list of all selected; default = '% selected'
//
// Dependencies:  jQuery 1.2.6 or higher (http://jquery.com/)
//
// Change Log:
//
//		1.0.1	- Updated to work with jQuery 1.2.6+ (no longer requires the dimensions plugin)
//				- Changed $(this).offset() to $(this).position(), per James' and Jono's suggestions
//				
//
// Licensing & Terms of Use
// 
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC. 
//	
*/
 
if(jQuery) (
 
function($){
 
	$.extend($.fn, {
		multiSelect: function(o, callback) {
			// Default options
 
			if( !o ) var o = {};
			if( o.selectAll == undefined ) o.selectAll = true;
			if( o.selectAllText == undefined ) o.selectAllText = "Select All";
			if( o.noneSelected == undefined ) o.noneSelected = 'Select options';
			if( o.oneOrMoreSelected == undefined ) o.oneOrMoreSelected = '% selected';
			//
 
			// Initialize each multiSelect
			$(this).not('.multiSelect').each( function() {
				var select = $(this);
				$('#'+select.attr('id')+ '_multiSelectOptions').remove();
 
 
				var html = '<input type="text" id="' +select.attr('id')+ '" name="' +select.attr('name')+ '" readonly="readonly" class="multiSelect '+select.attr('class')+'" value="" style="cursor: default;'+select.attr('style')+'" />';
				html += '<div align="left" id="' +select.attr('id')+ '_multiSelectOptions" class="multiSelectOptions" style="position: absolute; z-index: 99999; display: none;">';
				if( o.selectAll ) html += '<label class="selectAll"><input type="checkbox" class="selectAll" />' + o.selectAllText + '</label>';
				var PREVNAME='checkbox';
				select.find('OPTION').each( function() {
					if( $(this).val() != '' ) {
						var NAME=null;
						var RADIO="checkbox";
 
						//si l'on est en présence d'un groupe d'option
						if ($(this).hasClass('radio')){NAME=$(this).attr('radio');//RADIO="radio"
						}
 
						//if (PREVNAME=='checkbox' && RADIO=='radio'){html+="<fieldset>"}
						//if (PREVNAME=='radio' && RADIO=='checkbox'){html+="</fieldset>"}
						//
 
 
						html += '<label class="' +RADIO+ '" title="'+$(this).attr('title')+'" ><input type="' +RADIO+ '" class="'+NAME+'" name="' +NAME+ '"  value="' + $(this).val() + '"';
						if( $(this).attr('selected') ) html += ' checked="checked"';
						html += 'onclick="checkboxMulti(this,\''+NAME+'\')"/>' + $(this).html() + '</label>';//name="' + select.attr('name') + '_"
						//PREVNAME=RADIO; onclick="if (this.checked){$(this).attr(\'checked\',null)}else{$(this).attr(\'checked\',\'checked\')}"
					}
				});
				html += '</div>';
				select.after(html);
				$('#'+select.attr('id')+ '_multiSelectOptions').prependTo('body').css('top','-500px')
				$('#'+select.attr('id')+ '_multiSelectOptions').css('width',$('#'+select.attr('id')+ '_multiSelectOptions').outerWidth()+20+'px');
 
 
 
 
 
 
 
				// Events
				//select.next('.multiSelectOptions').data('blur',$(this).data('blur'));
 
				select.next('.multiSelect').mouseover( function() {
					$(this).addClass('hover');
				}).mouseout( function() {
					$(this).removeClass('hover');
				}).click( function() {
					// Show/hide on click
 
					if( $(this).hasClass('active') ) {
						$(this).multiSelectOptionsHide();
					} else {
						$(this).multiSelectOptionsShow();
 
					}
					return false;
				}).focus( function() {
					// So it can be styled with CSS
					$(this).addClass('focus');
				}).blur( function() {
					// So it can be styled with CSS
					$(this).removeClass('focus');
				});
 
				// Determine if Select All should be checked initially
				if( o.selectAll ) {
					var sa = true;
					$('#'+select.attr('id')+ '_multiSelectOptions').find('INPUT:checkbox,INPUT:radio').not('.selectAll').each( function() {
						if( !$(this).attr('checked') ) sa = false;
					});
					if( sa ) $('#'+select.attr('id')+ '_multiSelectOptions').find('INPUT.selectAll').attr('checked', true).parent().addClass('checked');
				}
 
				// Handle Select All
				$('#'+select.attr('id')+ '_multiSelectOptions').find('INPUT.selectAll').click( function() {
					if( $(this).attr('checked') == true ) $(this).parent().parent().find('INPUT:checkbox,INPUT:radio').attr('checked', true).parent().addClass('checked'); else $(this).parent().parent().find('INPUT:checkbox,INPUT:radio').attr('checked', false).parent().removeClass('checked');
				});
 
				// Handle checkboxes
				/*$('#'+$(this).attr('id')+ '_multiSelectOptions').find('INPUT:checkbox,INPUT:radio')
					.click( function() {
						$(this).parent().parent().multiSelectUpdateSelected(o);
						$(this).parent().parent().find('LABEL').removeClass('checked').find('INPUT:checked').parent().addClass('checked');
						$(this).parent().parent().prev('.multiSelect').focus();
						if( !$(this).attr('checked') ) $(this).parent().parent().find('INPUT:checkbox.selectAll').attr('checked', false).parent().removeClass('checked');
						if( callback ){ callback($(this));}
				});*/
 
				$('#'+select.attr('id')+ '_multiSelectOptions')
					.click( function(e) {
						$(e.target).parent().parent().multiSelectUpdateSelected(o);
						$(e.target).parent().parent().find('LABEL').removeClass('checked').find('INPUT:checked').parent().addClass('checked');
						$(e.target).parent().parent().prev('.multiSelect').focus();
						if( !$(e.target).attr('checked') ) $(e.target).parent().parent().find('INPUT:checkbox.selectAll').attr('checked', false).parent().removeClass('checked');
						if( callback ){ callback($(e.target));}
 
				});
 
 
				// Initial display
				$('#'+select.attr('id')+ '_multiSelectOptions').each( function() {
 
					$(this).multiSelectUpdateSelected(o);
 
					$(this).find('INPUT:checked').parent().addClass('checked');
				});
 
				// Handle hovers
				$('#'+select.attr('id')+ '_multiSelectOptions').find('LABEL').mouseover( function() {
					$(this).parent().find('LABEL').removeClass('hover');
					$(this).addClass('hover');
				}).mouseout( function() {
					$(this).parent().find('LABEL').removeClass('hover');
				});
 
				// Keyboard
				select.next('.multiSelect').keydown( function(e) {
					// Is dropdown visible?
					if( $('#'+$(this).attr('id')+ '_multiSelectOptions').is(':visible') ) {
						// Dropdown is visible
						// Tab
						if( e.keyCode == 9 ) {
							$(this).addClass('focus').trigger('click'); // esc, left, right - hide
							$(this).focus().next(':input').focus();
							return true;
						}
 
						// ESC, Left, Right
						if( e.keyCode == 27 || e.keyCode == 37 || e.keyCode == 39 ) {
							// Hide dropdown
							$(this).addClass('focus').trigger('click');
						}
						// Down
						if( e.keyCode == 40 ) {
							if( !$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL').hasClass('hover') ) {
								// Default to first item
								$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL:first').addClass('hover');
							} else {
								// Move down, cycle to top if on bottom
								$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL.hover').removeClass('hover').next('LABEL').addClass('hover');
								if( !$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL').hasClass('hover') ) {
									$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL:first').addClass('hover');
								}
							}
							return false;
						}
						// Up
						if( e.keyCode == 38 ) {
							if( !$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL').hasClass('hover') ) {
								// Default to first item
								$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL:first').addClass('hover');
							} else {
								// Move up, cycle to bottom if on top
								$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL.hover').removeClass('hover').prev('LABEL').addClass('hover');
								if( !$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL').hasClass('hover') ) {
									$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL:last').addClass('hover');
								}
							}
							return false;
						}
						// Enter, Space
						if( e.keyCode == 13 || e.keyCode == 32 ) {
							// Select All
							if( $('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL.hover INPUT:checkbox').hasClass('selectAll') ) {
								if($('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL.hover INPUT:checkbox').attr('checked') ) {
									// Uncheck all
									$('#'+$(this).attr('id')+ '_multiSelectOptions').find('INPUT:checkbox,INPUT:radio').attr('checked', false).parent().removeClass('checked');
								} else {
									// Check all
									$('#'+$(this).attr('id')+ '_multiSelectOptions').find('INPUT:checkbox,INPUT:radio').attr('checked', true).parent().addClass('checked');
								}
								$('#'+$(this).attr('id')+ '_multiSelectOptions').multiSelectUpdateSelected(o);
								if( callback ) callback($(this));
								return false;
							}
							// Other checkboxes
							if( $('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL.hover INPUT:checkbox').attr('checked') ) {
								// Uncheck
								$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL.hover INPUT:checkbox').attr('checked', false);
								$('#'+$(this).attr('id')+ '_multiSelectOptions').multiSelectUpdateSelected(o);
								$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL').removeClass('checked').find('INPUT:checked').parent().addClass('checked');
								// Select all status can't be checked at this point
								$('#'+$(this).attr('id')+ '_multiSelectOptions').find('INPUT:checkbox.selectAll').attr('checked', false).parent().removeClass('checked');
								if( callback ) callback($(this));
							} else {
								// Check
								$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL.hover INPUT:checkbox').attr('checked', true);
								$('#'+$(this).attr('id')+ '_multiSelectOptions').multiSelectUpdateSelected(o);
								$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL').removeClass('checked').find('INPUT:checked').parent().addClass('checked');
								if( callback ) callback($(this));
							}
						}
						return false;
					} else {
						// Dropdown is not visible
						if( e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 13 || e.keyCode == 32 ) { // down, enter, space - show
							// Show dropdown
							$(this).removeClass('focus').trigger('click');
							$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL:first').addClass('hover');
							return false;
						}
						//  Tab key
						if( e.keyCode == 9 ) {
							// Shift focus to next INPUT element on page
							$(this).focus().next(':input').focus();
							return true;
						}
					}
					// Prevent enter key from submitting form
					if( e.keyCode == 13 ) return false;
				});
				//console.log('>>'+select.attr('id')+ '_multiSelectOptions'+$('#'+select.attr('id')+ '_multiSelectOptions').size());
				// Eliminate the original form element
				select.remove();
			});
 
		},
 
 
		// Hide the dropdown
		multiSelectOptionsHide: function() {
			//if($(this).hasClass('active')){alert($(this).attr('id'))}
			$('#'+$(this).attr('id')+ '_multiSelectOptions').hide()
			$(this).removeClass('active').next('.multiSelectOptions').hide();
		},
 
		getPosition: function()
					{
 
						var left =0; //window.pageXOffset;
						var top =0; //window.pageYOffset;//0;
						var position='absolute';
						/*On récupère l'élément*/
 
						var e = document.getElementById($(this).attr('id'))
						/*Tant que l'on a un élément parent*/
						while (e.offsetParent != undefined && e.offsetParent != null)
						{
							if ($(e).css('position')=='fixed')
							{
								position='fixed';
							}
							/*On ajoute la position de l'élément parent*/
							left += e.offsetLeft + (e.clientLeft != null ? e.clientLeft : 0);
							top += e.offsetTop - e.scrollTop +(e.clientTop != null ? e.clientTop : 0);
							e = e.offsetParent;
 
							//alert(position);
						}
						return {'left':left,'top':top,'position':position};
					},
 
		getScrollPosition: function()
									{
									return Array((document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop);},
		// Show the dropdown
		multiSelectOptionsShow: function() {
			// Hide any open option boxes
			$('.multiSelectOptions').hide()
			$('.multiSelect').multiSelectOptionsHide();
			$('#'+$(this).attr('id')+ '_multiSelectOptions').find('LABEL').removeClass('hover');
			$('#'+$(this).attr('id')+ '_multiSelectOptions').show();// patch .not(':empty')
			$(this).addClass('active').next('.multiSelectOptions').show();// patch .not(':empty')
 
			// Position it
			var offset = $(this).getPosition();//$(this).position();
			var offset2 = $(this).getPosition();
			$('#'+$(this).attr('id')+ '_multiSelectOptions').css('position',offset.position)
			//$('#'+$(this).attr('id')+ '_multiSelectOptions').outerHeight()+'#'+offset2.top+'#'+screen.height-$(this).getScrollPosition()
			//alert(window.innerHeight )
 
			if (($('#'+$(this).attr('id')+ '_multiSelectOptions').outerHeight()+offset2.top)<window.innerHeight)
			{
			$('#'+$(this).attr('id')+ '_multiSelectOptions').css({ top:  offset.top + $(this).outerHeight() + 'px' });
			}
			else
			{
				$('#'+$(this).attr('id')+ '_multiSelectOptions').css({ top:  (offset.top - $('#'+$(this).attr('id')+ '_multiSelectOptions').outerHeight() ) + 'px' });
			}
 
 
			if (($('#'+$(this).attr('id')+ '_multiSelectOptions').outerWidth()+offset2.left)<window.innerWidth){
			$('#'+$(this).attr('id')+ '_multiSelectOptions').css({ left: offset.left + 'px' });}
			else{$('#'+$(this).attr('id')+ '_multiSelectOptions').css({ left: (offset.left+$(this).outerWidth()-$('#'+$(this).attr('id')+ '_multiSelectOptions').outerWidth()) + 'px' });}
 
			// Disappear on hover out
			multiSelectCurrent = $(this);
			var timer = '';
			$('#'+$(this).attr('id')+ '_multiSelectOptions').hover( function() {
				clearTimeout(timer);
			}, function() {
				timer = setTimeout('$(multiSelectCurrent).multiSelectOptionsHide(); $(multiSelectCurrent).unbind("hover");', 100);
			});
 
		},
 
		// Update the textbox with the total number of selected items
		multiSelectUpdateSelected: function(o) {
 
			var i = 0, s = '';
			var parentid=$(this).attr("id").split('_')[0];
			//alert(parentid);
			$(this).find('INPUT:checkbox:checked,INPUT:radio:checked').not('.selectAll').each( function() {
				i++;
			})
 
			if( i == 0 ) {
				$(this).prev('INPUT.multiSelect').val( o.noneSelected );
				$("INPUT#"+parentid+'.multiSelect').val( '' ).attr('title','');
			} else {
				if( o.oneOrMoreSelected == '*' ) {
					var display = '';
					$(this).find('INPUT:checkbox:checked,INPUT:radio:checked').each( function() {
 
						if( $(this).parent().text() != o.selectAllText ) display = display + $(this).parent().text() + ', ';
					});
					display = display.substr(0, display.length - 2);
 
					//$(this).prev('INPUT.multiSelect').val( display );
					//alert(display);
					$("INPUT#"+parentid+'.multiSelect').val( display ).attr('title',display);
				} else {
					//$(this).prev('INPUT.multiSelect').val( o.oneOrMoreSelected.replace('%', i) );
					$("#"+parentid).val( o.oneOrMoreSelected.replace('%', i) );
				}
			}
		}
 
	});
 
})(jQuery);
 
var addItemMultiSelect=function(obj){
 
	var newItem=prompt('Saisissez la valeur \xe0 ajouter');
 
	if (newItem){
	var htmlNewItem=$('<label title="" class="checkbox"><input checked="checked" type="checkbox" value="'+newItem+'" name="null">'+newItem+'</label>')
	$('#'+obj+'_multiSelectOptions').append(htmlNewItem);
 
	}
	$('#'+obj).multiSelect('toto');
 
 
 
};