I create the HTML for a SELECT
list (it's not a dropdown!) and its OPTION
s through Java (8):
static final String font="style=\"font-family: 'Courier New'\"";
out.println("<SELECT name='l' id='selectList' size=19 style='width: 400px'>\n");
out.println("<OPTION value='"+value+"' "+font+" >"+someText+"</OPTION>");
The HTML then looks like this:
<SELECT name='l' id='selectList' size=19 style='width: 400px'>
<OPTION value='0' style="font-family: 'Courier New'" >Some Text</OPTION>
Next to the list there's also an "Append" button that appends more elements through JavaScript:
var selectList = document.getElementById("selectList");
var option = new Option(mytext,myvalue);
selectList.appendChild(option);
Problem: This new option doesn't use the "Courier New" font.
My "styles.css" file already has a custom "h3" and "h4" but I now also want to create a custom "substyle" that I can then set the option in both HTML and Javascript to. Padding, font size,... should all be the default, I only want to set the font-family
of these specific options to "Courier New" and do it in a way that if I want to change it again later on, I only have to do it in a single spot (the css). This style should only affect the options I use it on, the others should all use the default style or whatever I set them to.
Question: How do I accomplish this (without adding an extra library)?
I'm aware there are already a bunch of questions asking similar things but they're usually at least 5 years old, some refer to dropdowns and some even say that it's not possible to change what an option looks like, which changed in the last couple of years.
What I've tried:
Java/HTML:
out.println("<OPTION value='"+value+"' "+"class=\"somethingsomething\""+" >"+someText+"</OPTION>");
//<OPTION value='0' class="somethingsomething" >Some Text</OPTION> //Result
CSS:
.somethingsomething { //Also tried "option.somethingsomething"
font-family: 'Courier New', monospace;
}
This doesn't seem to work (tested with Firefox), the options use the default font.