aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Creating a table cell with text containing an apostrophe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Creating a table cell with text containing an apostrophe" Watch "Creating a table cell with text containing an apostrophe" New topic
Author

Creating a table cell with text containing an apostrophe

Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 31, 2001
Posts: 282
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

Joined: Jan 10, 2002
Posts: 61761
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
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

Joined: Jan 10, 2002
Posts: 61761
    
  67

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

Joined: Jul 31, 2001
Posts: 282
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a table cell with text containing an apostrophe