views:

302

answers:

2

I can't seem to find the correct way to take some html being returned from the server, replace each ' with &apos and finally - append the new html to the DOM.

Before the modification, the below works just fine to append the html (not altered)

$(xhtml).find("#inner").appendTo("#appendTD");

But now that I want to alter the xhtml first, the below doesn't seem to work as expected

var html = $(xhtml).find("#gridFormInformation").find("'").replaceWith("'");
$(html).find("#inner").appendTo("#appendTD");

As you can see from the above, I want to replace each ' w/ the &apos inside a table element w/ the id #gridFormInformation. Part of this doesn't work because the html obj i'm trying to append is a jQuery object (not the simple string that I append in the first working example)

Any help?

+3  A: 
xhtml.replace(/'/g, ''');
$(xhtml).find("#inner").appendTo("#appendTD");

The find and replaceWith methods are not meant to be used as you're using them. They are jQuery functions to find and replace elements based on CSS selectors. You need to use the native replace function of the string and then perform the jQuery.

Paolo Bergantino
Actually, I think you need xhtml.replace(/'/g, ''');
Flavius Stef
Correct. Thanks.
Paolo Bergantino
A: 

find() expects a CSS valid expression, which "'" is not. Try using replace(). http://www.w3schools.com/jsref/jsref_replace.asp

Flavius Stef