Thomson
Thomson

Reputation: 143

Replace Single Quotes in Javascript or JQuery

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

Upvotes: 1

Views: 1277

Answers (2)

Marcos Buarque
Marcos Buarque

Reputation: 3418

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 ' or the HTML code you prefer. Reference: http://www.degraeve.com/reference/specialcharacters.php

Upvotes: 0

nickf
nickf

Reputation: 546503

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);

Upvotes: 2

Related Questions