Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a table cell with text containing an apostrophe

 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am dynamically creating a table within a web page. I know how to do that. However, some of data names contain an apostrophe, e.g. "O'Brien". I use insertRow/insertCell to create new rows and columns but when I attempt to set innerHTML on a cell for one of these data names it fails.

If I replace the apostrophe, it works fine, e.g.

I also tried to just create a text node and append it to the cell, e.g.

That does not seem to work either. Why do these operations fail for data names with apostrophes? Is there a workaround?
[ September 05, 2008: Message edited by: Jay Damon ]
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It fails....

It fails how? Computer catches on fire, error message, does not show up?

Sounds like to me you have a case of the unescaped blues. Are you doing something like 'O'Brien'.

Eric
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The text is not assigned to the cell. The loop fails on the error and only the names already inserted into the table are displayed.

Are saying that this should work? The text is coming straight from a JavaScript array where all elements are enclosed in double quotes, e.g.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it should work. Apostrophe is no different from any other character when it comes to data. The problems that can be introduced are syntactic, and only arise when creating string literals and attribute values.

At this point you'll probably need to pare down a code example to the simplest sample that demonstrates the issue and post that code.
[ September 05, 2008: Message edited by: Bear Bibeault ]
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not a syntax error. It will work when I replace the apostrophes. Re: Eric's post, I have experimented with escaping the problem names.

fails as described before.

works but the name is displayed as "James O\'Brien"

For now I have settled on replacing the apostrophe with a space.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With replace(), you are running into regular expression syntax problems as well. I didn't think that replace() was part of the original problem.

If you don't want to post code, not much more that can be said.
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. I've arrived at a solution that works for me. I found that I had problems with double quotes as well. The solution was to use the Java String replaceAll method to replace the offending characters before the JavaScript array is created.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic