Last active
January 8, 2025 23:42
-
-
Save crabshank/c45a76d35e28b44a5e362a3b72d33f8d to your computer and use it in GitHub Desktop.
Insert multi-select checkboxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let choics=['A','B','C']; // Your options | |
| let optns=[]; | |
| let opts=''; | |
| for (let i=0, len=choics.length; i<len;++i){ | |
| let opi='opt'+i; | |
| let s=`<label style="display: inline-flex;"> | |
| <input type="checkbox"></input>${choics[i]}</label>`; | |
| optns.push(s); | |
| } | |
| opts=optns.join('\n'); | |
| document.body.insertAdjacentHTML('afterbegin',`<div style="width: max-content"> | |
| <div style="position: relative;min-width: 17.7ch;"> | |
| <select style="width: 100%;font-weight: bold;"> | |
| <option style="color:black">Select types</option> | |
| </select> | |
| <div style="position: absolute;left: 0;right: 0; top: 0; bottom: 0;"></div> | |
| </div> | |
| <div style="border: buttonface 0.2ch outset;display: flex !important;flex-direction: column !important;"> | |
| ${opts} | |
| </div> | |
| </div>`); | |
| let sels=document.body.firstElementChild; | |
| let sopt=sels.getElementsByTagName('OPTION')[0]; | |
| let cOpts=[...sels.getElementsByTagName('INPUT')]; | |
| let el=…;//Place checkboxes around element | |
| el.insertAdjacentElement('…',sels); // Place checkboxes | |
| let p=[...sels.getElementsByTagName('INPUT')];//All checkboxes | |
| let toFilt=[...document.querySelectorAll('…')]; //To filter with checkboxes | |
| let choiceInp=()=>{ // On check! | |
| let toDisplay=[]; | |
| p.forEach( (opt)=>{ // Each checkbox option | |
| let ptx=opt.parentElement.innerText; // Checkbox label text | |
| if(opt.checked){ | |
| toFilt.forEach((u,x)=>{ | |
| if(!toDisplay.includes(x) && //Mandatory condition | |
| … // Insert user condition(s) here! | |
| ){ | |
| toDisplay.push(x); | |
| } | |
| }); | |
| } | |
| }); | |
| for (let i=0, len_i=toFilt.length; i<len_i;++i){ | |
| let iflt=toFilt[i]; | |
| if(toDisplay.includes(i)){ | |
| //iflt.style.display='block'; | |
| }else{ | |
| //iflt.style.display='none'; | |
| } | |
| } | |
| }; | |
| cOpts.forEach(c=>{ | |
| c.oninput=choiceInp; | |
| }); | |
| p.forEach((i,x)=>{ | |
| i.checked=true; | |
| }); | |
| choiceInp(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment