mardi 12 mars 2019

Add checkbox to select options [duplicate]

This question already has an answer here:

I have a select box element that I need to add a checkbox in front of each option. It doesn't need to be an actual input, just needs to appear as a checkbox and become checked when the option is selected.

I can't change the markup as this needs to be executed as a test via JS/JQ post DOM.

 <select class="filter">
 <option value="1">Option 1</option>
 <option value="2">Option 2</option>
 <option value="3">Option 3</option>
 <option value="4">Option 4</option>
 <option value="5">Option 5</option>
 </select>

Ideally i'd have something like this:

<option value="1"><input type="checkbox" value="1"/>Option 1</option>
<option value="2"><input type="checkbox" value="2"/>Option 2</option>

$('<input />', { type: 'checkbox', value: getFromOption }).appendTo('.filter  option');

I realize this is not a proper markup so I'm looking for additional options. Is there any way to 'fake' a checkbox in front of a select option? I have already tried converting the options to actual inputs/labels but it caused issues...




Aucun commentaire:

Enregistrer un commentaire