Do I need to escape quotes inside of an html attribute value? What characters are allowed?
Is this valid?
<span title="This is a 'good' title.">Hi</span>
The purpose of the HTML value attribute is to specify the current value for an input type. For those elements that can display their values (such as text fields), they will display this value onscreen. Otherwise, the values are all available as form values when submitted.
If your attribute value is quoted (starts and ends with double quotes "
), then any characters except for double quotes and ampersands are allowed, which must be quoted as "
and &
respectively (or the equivalent numeric entity references, "
and &
)
You can also use single quotes around an attribute value. If you do this, you may use literal double quotes within the attribute: <span title='This is a "good" title.'>...</span>
. In order to escape single quotes within such an attribute value, you must use the numeric entity reference '
since some browsers don't support the named entity, '
(which was not defined in HTML 4.01).
Furthermore, you can also create attributes with no quotes, but that restricts the set of characters you can have within it much further, disallowing the use of spaces, =
, '
, "
, <
, >
, `
in the attribute.
See the HTML5 spec for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With