I do know that the HTML is no longer interesting for the major half of the developers but still sometimes you need to use it. Yesterday I’ve encountered one thing that after reading specs made me laugh a lot. I’m talking about “selected” attribute of the “option” tag.
In any programming language (any general language for logical people) normal usage of attributes that are meant by their nature to bee boolean is having 2 possible values to be set: true or false.
“selected” attribute in HTML is a bit different
To not write a lot here is a receipt:
Specifies that this option will be pre-selected when the user first loads the page.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either selected or selected="selected").
Possible values:
* [Empty string]
* selected
So you either specify
<option value="fuck" selected="selected">it</option>
if you want this option to be selected or you should miss the attribute at all. I think this should have been a joke from the dude who created this ))
UPDATE:
I would not post this if this inconvenience did not impact the thing I am doing. It imapcts so I decided to write a little JSP custom tag that fixes the problem in a nice way:
@SuppressWarnings("serial")
public class OptionsTag extends TagSupport {
public void setDataProvider(List value)
{
this.dataProvider = value;
}
public void setItemToSelect(DataItem value)
{
this.itemToSelect = value;
}
public int doStartTag()
{
try
{
JspWriter out = pageContext.getOut();
String selected;
for(DataItem item: dataProvider)
{
if (itemToSelect != null)
{
selected = itemToSelect.isEqual(item)?"selected='selected'":"";
}
else
{
selected = "";
}
out.println("" + item.getName() + "");
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
return Tag.SKIP_BODY;
}
private List dataProvider;
private DataItem itemToSelect;
}
The usage of the tag in JSPs is straightforward:
<c:if test=”<%=activityCategories!=null%>”>
<select name=”categoryId”>
<components:options dataProvider=”<%=activityCategories%>” itemToSelect=”<%=currentActivityCategory%>”/>
</select>
</c:if>
Recent Comments