cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>Select multiple displayed with dropdown</h1>
Normal:
<select id="monselect" multiple title="Placeholder" >
  <option value="2">option deux</option>
  <option value="4">option quatre</option>
  <option value="6">option six</option>
  <option value="8" disabled>disabled option</option>
</select>

<hr>
Disabled: <select id="monselect2" multiple disabled>
  <option value="2">option deux</option>
  <option value="4" selected>option quatre</option>
  <option value="6" selected>option six</option>
</select>


<hr>
Backstage:
<input type="button" onclick="document.getElementById('monselect3').style.display='';" value="Display hidden select"><br>
<select id="monselect3" multiple>
  <option value="2">option deux</option>
  <option value="4" selected selected>option quatre</option>
  <option value="6" selected selected>option six</option>
</select>

            
          
!
            
              multiselect_to_dropdown('monselect', 'sélectionnés');
multiselect_to_dropdown('monselect2');
multiselect_to_dropdown('monselect3');



function multiselect_to_dropdown(eleid, multipletext) {
	/* par keul (keul.fr) CC0 (https://codepen.io/keul/pen/bdaGPK)
	param: id of select multiple, text displayed with a 2+ selection */
	if(!multipletext) multipletext='selected';

	var myele = document.getElementById(eleid);
	var opts=myele.options;
	if(arguments.length==3 && arguments[2]=='blur') {
		clearTimeout(myele.dataset.timer);
    myele.dataset.timer=setTimeout(function(){
			if(document.activeElement.id!=eleid+'_sk_input' && document.activeElement.id!=eleid+'_sk_span') 
				document.getElementById(eleid+'_sk_span').style.display="none";
		},200);
		return;
	}
	if(arguments.length==3 && arguments[2]=='ckbox') {
		var select_qty=0;
		var selectname='';
		for(var i=0,l=opts.length; i<l; i++) {
			myele.options[i].selected=document.getElementById(eleid+'_sk_'+i).checked
			if(document.getElementById(eleid+'_sk_'+i).checked) {
				++select_qty;
				selectname=opts[i].innerHTML;
			}
			if(select_qty>1) {
				document.getElementById(eleid+'_sk_input').value=select_qty+' '+multipletext;
			} else if(select_qty==1) {
				document.getElementById(eleid+'_sk_input').value=selectname.trim();
			} else {
				document.getElementById(eleid+'_sk_input').value='';
			}
		}
		return;
	}
	
	var sp2 = document.createElement('input');
	sp2.setAttribute('id', eleid+'_sk_input');
	sp2.setAttribute('type', 'text');
	if(myele.title) {
		sp2.setAttribute('placeholder', myele.title);
	}
	sp2.setAttribute('autocomplete', 'off');
	sp2.setAttribute('readonly', 'readonly');
	sp2.setAttribute('style', 'width:'+myele.offsetWidth+'px');
	sp2.setAttribute('onfocus', 'document.getElementById("'+eleid+'_sk_span").style.display="";');
	sp2.setAttribute('onblur', 'multiselect_to_dropdown(\''+eleid+'\',\''+multipletext+'\',\'blur\');');
	myele.parentNode.insertBefore(sp2, myele.nextSibling);

	var sp3 = document.createElement('span');
	sp3.setAttribute('id', eleid+'_sk_span');
	sp3.setAttribute('onmouseout', 'multiselect_to_dropdown(\''+eleid+'\',\''+multipletext+'\',\'blur\');');
	sp3.setAttribute('onclick', 'document.getElementById("'+eleid+'_sk_input").focus();');
	sp3.setAttribute('style', 'min-width:'+sp2.offsetWidth+'px;margin-top:'+sp2.offsetHeight+'px;margin-left:-'+sp2.offsetWidth+'px;position:absolute;border:1px solid grey;display:none;z-index:9999;text-align:left;background:white;max-height:130px;overflow-y:auto;overflow-x: hidden;');
	sp2.parentNode.insertBefore(sp3, sp2.nextSibling);

	var tmp='';
	var dis='';
	for(var i=0,l=opts.length; i<l; i++) {
		var slected=(opts[i].selected)?'checked':'';
		dis=myele.disabled?'disabled':'';
		dis=(opts[i].disabled)?'disabled':dis;
		tmp+='<label  style="display:inline;white-space:nowrap;" for="'+eleid+'_sk_'+i+'"><input style="width:auto;" type="checkbox" '+slected+' id="'+eleid+'_sk_'+i+'" onchange="multiselect_to_dropdown(\''+eleid+'\',\''+multipletext+'\',\'ckbox\')" '+dis+'>'+opts[i].innerHTML+'</label><br>';
	}
	sp3.innerHTML=tmp;
	myele.style.display='none';
	multiselect_to_dropdown(eleid,multipletext,'ckbox');
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console