Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to put <span /> tags inside <option /> tags, only for string manipulation not styling?

Tags:

html

option

tags

I would like to make groups of the text content of an <option /> tag. Say I have the following: <option>8:00 (1 hour)</option>, the time pattern 8:00 can be modified, then the text in parenthesis (1 hour) can also be modified.

I was thinking of doing something like

<option>   <span>8:00</span>   <span> (1 hour)</span> </option> 

Is it bad to put <span /> tags inside <option /> tags, only for string manipulation not styling?

like image 880
Rubens Mariuzzo Avatar asked Apr 15 '11 15:04

Rubens Mariuzzo


People also ask

When would you use a span tag?

Span tags are used on small segments of text, links, images, and other HTML elements that appear inline with the surrounding content. To summarize, a div tag creates a block-level element while a <span> tag wraps around an inline element.

Where do we use span tag in HTML?

The <span> tag is an inline container used to mark up a part of a text, or a part of a document. The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element.

Why do we use span tag in HTML?

The <span> HTML element is a generic inline container for phrasing content, which does not inherently represent anything. It can be used to group elements for styling purposes (using the class or id attributes), or because they share attribute values, such as lang .

What is the use of SPAN tag give an example?

The span tag is used for the grouping of inline elements & this tag does not make any visual change by itself. span is very similar to the div tag, but div is a block-level tag and span is an inline tag. Example 1: In this example, we simply use span tag with style in HTML.


2 Answers

From the HTML 5spec:

Content model:

  • If the element has a label attribute and a value attribute: Nothing.
  • If the element has a label attribute but no value attribute: Text.
  • If the element has no label attribute and is not a child of a datalist element: Text that is not inter-element whitespace.
  • If the element has no label attribute and is a child of a datalist element: Text.

So depending on context there are two things that you can put inside an <option> — text or nothing at all — you may not put a <span> or any other element there.


From the HTML 4.01 spec:

<!ELEMENT OPTION - O (#PCDATA)         -- selectable choice --> 

(Even the HTML 3.2 and HTML 2 specs say: <!ELEMENT OPTION - O (#PCDATA)*>)


An option element cannot have any child elements. So yes, it is bad.

like image 64
Quentin Avatar answered Sep 23 '22 01:09

Quentin


You can use a Javascript plugin to overcome this limitation. For example jQuery plugin "Select2" Select2 plugin homepage. I use it in a couple of my projects and think that it's pretty flexible and convenient.

There are a number of them, but they do quite same thing - convert traditional <select> into <div> blocks with an extra functionality.

like image 25
Ivan Yaremchuk Avatar answered Sep 22 '22 01:09

Ivan Yaremchuk