My problem comes from HTML dropdown select not able to preserve multiple consecutive whitespaces in its options. Here is a sample testcase and outcome. The \"white-space:pre
The accepted answer is correct when dynamically creating the SELECT list from a server-side language. However, if you would like to create the options list in Javascript using the document.createElement function (probably the safest way to do so when using input you can't always completely control) then you can use the following method:
var someSelectElement = document.getElementById("id_of_select_element");
var optionSt = "Some spaces go here.";
var tObj = document.createElement("OPTION");
tObj.value = 1; // whatever value you need.
tObj.title = "Whatever title you want to appear when hovering over the option";
tObj.text = decodeURI(optionSt.replace(/ /g, "%C2%A0")); // decodeURI is the key here.
// Then add it to an already existing HTML select element...
someSelectElement.appendChild(tObj);
Form inputs usually map to their relevant native OS controls so they can be hard to style. The usual workaround to keep whitespace from collapsing into 1 space in <option>
s is to use
instead of usual spaces.
eg. one of your options would be:
<option> Any </option>
For more custom appearance, you would hide the real select element completely and replace it with custom markup and javascript.