Re: HTML entities not rendered when appended to SELECT
- From: Thomas 'PointedEars' Lahn <PointedEars@xxxxxx>
- Date: Thu, 25 Nov 2010 17:55:21 +0100
Web Interface Tricks wrote:
On Nov 25, 12:28 pm, Patrick Nolan <p...@xxxxxxxxxxxxxxxxxxx> wrote:
I have a web page which has lists of names in SELECT elements.
It uses javascript to move the names from one list to another.
All is OK except for some names which contain special characters
represented as HTML entities. The original HTML is built server-
side with the SELECT elements filled with HTML <option> tags,
and that works OK. But when javascript creates a new Option element
and inserts it into a SELECT, the HTML entities are displayed
literally, not rendered as they should be.
A simple example:
<html>
<head>
<title>Two different ways to populate an Option</title>
<script type="text/javascript">
function addOpt() {
var t = document.getElementById("x");
var opt = new Option();
t.appendChild(opt);
You want to move this statement below the following two assignments, so that
you append a finished `option' element. However, you should not use the
appendChild() method, for it is not interoperable there, but assign to
`t.options[t.options.length]' instead.
opt.text = "Ismaël";
opt.value="b";}
</script>
</head>
<body onLoad="addOpt();">
<select size=2 name="x" id="x">
<option value="a">Ismaël</option>
</select>
</body>
</html>
This produces a page with a two-line SELECT box.
The name of a person appears in each line. In the first
line, ë displays as an "e" with a diaeresis over it,
as it should. In the second line it shows up literally
as "ë".
The same thing happens in Firefox 3.6.12, IE8 and Opera 10.53.
Is there some way to prevent this behavior?
Yes there is. You can enter html characters directly when creating
strings in javascript. Even if you put them into html elements.
s/can/MUST, except if it is XHTML and the content is not declared CDATA, or
if the proprietary `innerHTML' property is used (not recommended).
And JFTR: There are no "HTML characters". `ë', for example, is a
character *entity* *reference*, for the *numeric* *character* *reference*
`ë', which represents a character from the Universal Character Set,
which is character-by-character equivalent to *Unicode*.
<http://www.w3.org/TR/html401/charset.html>
Instead of:
new_option.value = "<";
you can do:
new_option.value = "<";
Note that this is syntactically invalid if used within an XHTML `script'
element which content is not declared CDATA. <http://validator.w3.org/>
And please get a real name. This is a discussion group where *people* are
posting, not organizations or Web sites.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
.
- Follow-Ups:
- Re: HTML entities not rendered when appended to SELECT
- From: Eric Bednarz
- Re: HTML entities not rendered when appended to SELECT
- From: Web Interface Tricks
- Re: HTML entities not rendered when appended to SELECT
- References:
- HTML entities not rendered when appended to SELECT
- From: Patrick Nolan
- HTML entities not rendered when appended to SELECT
- Prev by Date: Re: HTML entities not rendered when appended to SELECT
- Next by Date: Re: HTML entities not rendered when appended to SELECT
- Previous by thread: Re: HTML entities not rendered when appended to SELECT
- Next by thread: Re: HTML entities not rendered when appended to SELECT
- Index(es):
Relevant Pages
|