473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

createTextNode/innerHTML and special character handling

Hello,

I was wondering what is the correct way to handle special characters
via javascript and the DOM. I would like to avoid document.write and
innerHTML. What I am doing is dynamically creating options for a
select. The innerHTML example below works for firefox and internet
explorer, but is this the accepted way of dynamically adding special
characters.

option = document.createElement("OPTION");
// option.appendChild(document.createTextNode("&eacut e; - example"));
option.innerHTML = "é - example";

Thanks,
Dan Andrews

Jun 4 '07 #1
4 19500
Dan Andrews wrote:
option = document.createElement("OPTION");
// option.appendChild(document.createTextNode("&eacut e; - example"));
JavaScript supports string literals with Unicode characters so you can
simply use that character in a string literal e.g.
option.appendChild(document.createTextNode('é - example'));
Then there is String.fromCharCode e.g.
document.createTextNode(String.fromCharCode(233) + ' - example')
and there are Unicode escape sequences
document.createTextNode('\u00E9 - example')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 4 '07 #2
On Jun 4, 7:18 pm, Martin Honnen <mahotr...@yahoo.dewrote:
Dan Andrews wrote:
option = document.createElement("OPTION");
// option.appendChild(document.createTextNode("&eacut e; - example"));

JavaScript supports string literals with Unicode characters so you can
simply use that character in a string literal e.g.
option.appendChild(document.createTextNode('é - example'));
Then there is String.fromCharCode e.g.
document.createTextNode(String.fromCharCode(233) + ' - example')
and there are Unicode escape sequences
document.createTextNode('\u00E9 - example')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Yes, and createTextNode( "&eacute; - ..." ) will create text
"&eacute;" visible in the option (which you didn't want, for granted).
What you don't see difference between are the regular text fragments
and xml entities - entities are elements in xml which are recognized
by the famous "&" and ";" delimiters. What you should do is find the
javascript function that can make entities e.g.
document.createEntityReference( "eacute" ), but this I had problems
with - got exceptions etc. in some previous work, so I stuck with the
innerHTML property. I think you have to declare the entity first, then
get this reference etc. Anyway, if you get any idea how this works -
share it with us ;-)

Take a look at this, it probably contains the needed information
*somewhere* inside, but I don't have much time at the moment to read
all of it. Good luck:
http://www.w3.org/TR/2004/REC-DOM-Le...0407/core.html

Jun 4 '07 #3
On Jun 4, 11:18 am, Martin Honnen <mahotr...@yahoo.dewrote:
Dan Andrews wrote:
option = document.createElement("OPTION");
// option.appendChild(document.createTextNode("&eacut e; - example"));

JavaScript supports string literals with Unicode characters so you can
simply use that character in a string literal e.g.
option.appendChild(document.createTextNode('é - example'));
Then there is String.fromCharCode e.g.
document.createTextNode(String.fromCharCode(233) + ' - example')
and there are Unicode escape sequences
document.createTextNode('\u00E9 - example')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Thanks this is very good to know, however, I don't quite have
literals. I have both entity references and some unicode ("Anhui
\u5b89\u5fbd", "&Ntilde;uble",...) in my real situation. The text is
actually coming from a database via an XMLHttpRequest object and the
responseText. I believe these literals are interpreted a bit
differently when they are hard coded as in your '\u00E9 - example'
versus when they are sent across the wire in an XMLHttpRequest, or is
there a way to specify the characterset in an XMLHttpRequest. The
innerHTML is working well if I convert \u5b89 to &#xu5b89;, but I
still would like to avoid innerHTML.

[snip]
request = new XMLHttpRequest();
request.open("GET", "countries?fn=regions&id=" +
countryTypeIdSelected, true);
request.onreadystatechange = function() {
var i;
var array;
var option;
var select = document.getElementById("region");
var cdata;
if (request.readyState == 4) {
array = request.responseText.split(',');
if(array.length 1){
for(i=0; i < array.length - 1; i = i + 2){
option =
document.createElement("OPTION");
option.setAttribute("value", "" +
array[i]);
if(countryTypeIdSelected && countryTypeIdSelected
=== array[i]){
option.setAttribute("selected", "selected");
}
//option.appendChild(document.createTextNode("" +
array[i+1]));
option.innerHTML = "" + array[i+1];
select.appendChild(option);
select.disabled = false;
}
}
[snip]

Jun 4 '07 #4
ASM
Dan Andrews a écrit :
>
still would like to avoid innerHTML.

[snip]
request = new XMLHttpRequest();
request.open("GET", "countries?fn=regions&id=" +
countryTypeIdSelected, true);
request.onreadystatechange = function() {
var i;
var array;
var option;
var select = document.getElementById("region");
var cdata;
if (request.readyState == 4) {
array = request.responseText.split(',');
if(array.length 1){
for(i=0; i < array.length - 1; i = i + 2){
option = new Option(array[i+1],array[i]);
if(countryTypeIdSelected &&
countryTypeIdSelected === array[i])
option.selected = true;
select.options[select.length] = option;
}
select.disabled = false;
}
}
[snip]
It is not because you use XMLHttpRequest that old and eficient JS is dead.

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jun 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
7
by: Roy W. Andersen | last post by:
I've been searching google about this for days but can't find anything, so I'm hoping someone here can help me out. I'm trying to create zip-files without needing the zip-file extension in PHP,...
2
by: Avi U | last post by:
I'm creating a DOM document and I'm looking for a way to get an exception when I use ileagal characters. The method DOMElement->createElement() throw exception when I have an ileagal unicode...
11
by: DKM | last post by:
I have an empty tag like <p id="pid"></p> in a xml file. And, I have a script as foolows: str = "<b>Hello World!!!</b>"; ele = documeny.getElementById("pid");...
9
by: Hallvard B Furuseth | last post by:
Why does the FAQ (Q 4.15) recommend innerHTML when so many here say one should use createElement(), replaceChild() etc? Also, why does the "Alternative DynWrite function" at...
1
by: sonald | last post by:
Dear All, I am working on a module that validates the provided CSV data in a text format, which must be in a predefined format. We check for the : 1. Number of fields provided in the text file,...
50
by: Randy Webb | last post by:
When running this code in IE7: function insertScript() { var newScript = document.createElement('script'); newScript.type = "text/javascript"; var s = document.createTextNode("alert('hi');");...
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
1
kaleeswaran
by: kaleeswaran | last post by:
Hi! i am handling password generation module..... i need to force the user enter the special character in their password thing.so i need to handle the special character.so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.