views:

60

answers:

2

Hi All, i have an Html String in which i have some elements having single quotes.When i put this inside a $('varHtml'); Since the varHtml already contains some single quotes it qives an error, Can Somebody help me how to Escape the single quotes in the varHtml

Thanks in Advance

Thomson

+2  A: 

If you have a HTML string in a variable, then you don't need to put it in quotes:

var varHtml = "<div id='foo'></div>";
$(varHtml);
nickf
This is the string which i am having varHtml = "<img title="Go Search" onmouseover="this.src='/Style%20Library/Images/sg_search_but.gif'" onmouseout="this.src='/Style%20Library/Images/sg_search_but.gif'" alt="Go Search" src="/Style%20Library/Images/sg_search_but.gif" border="0"></a>" if i add a double quote in forming the string all the attributes will cause a problem if i add a single quote in forming the string the onmouseover and onmouseout attribute will create trouble –
Thomson
A: 

Hi, javascript lacks something like an htmlencode to run client side. So you will have to use one of the script libraries. You can try this jQuery solution: http://www.edentity.ca/WhoWeAre/Blog/Easy-Client-Side-html-EncodeDecode-using-jQuery.aspx Or you could simply use a javascript string replace function like the one explained here: http://www.w3schools.com/jsref/jsref_replace.asp. Replace ' with &#39; or the HTML code you prefer. Reference: http://www.degraeve.com/reference/specialcharacters.php

Marcos Buarque
nickf
This is the string which i am havingvarHtml = "<img title="Go Search" onmouseover="this.src='/Style%20Library/Images/sg_search_but.gif'" onmouseout="this.src='/Style%20Library/Images/sg_search_but.gif'" alt="Go Search" src="/Style%20Library/Images/sg_search_but.gif" border="0"></a>"if i add a double quote in forming the string all the attributes will cause a problemif i add a single quote in forming the string the onmouseover and onmouseout attribute will create trouble
Thomson