JoJo
JoJo

Reputation: 4923

How do I get the single quotes to remain in the variable?

This is a combination of classic ASP and Javascript, we are trying to insert a single quote around each itemno but it is not working.

It is ending up looking like this: 10234,12343, instead of '10234','12343',

(The trailing comma I strip later)...

What can be done differently to keep the ItemNo's single quoted?

var Prefix_JS_Item = [<%For intA = 1 to intCount%><%="'" & trim(Mid(cartobj(intA).Item("ItemNo"),4)) & "',"%><%Next%>];
document.write(Prefix_JS_Item);

Upvotes: 1

Views: 170

Answers (3)

amosrivera
amosrivera

Reputation: 26514

The script is doing what you want. The way you verify is what is wrong.

After ASP executes you will have '10234','12343' inside the JavaScript. To see it go to "view source code" (CTRL + U) in your browser.

Then you write it to the document. To JavaScript '10234' the quotes are delimiters for a string. Therefore it will only write what is inside them.

If you still want to have quotes even when writing it to the document then:

... <%="""'" &        trim(Mid(cartobj(intA).Item("ItemNo"),4)) & """," %>
document.write(Prefix_JS_Item);

Which in javascript will look like: "'10234'","'12343'"

EDIT: To scape quotes in ASP you use another quote.

Upvotes: 2

AnthonyWJones
AnthonyWJones

Reputation: 189437

There have been two attempts so far but try this:-

var Prefix_JS_Item = [<%For intA = 1 to intCount%><%="""'" & trim(Mid(cartobj(intA).Item("ItemNo"),4)) & "'"","%><%Next%>]; 
document.write(Prefix_JS_Item); 

Javascript supports both " and ' to enclose a string literal. So to include a ' in a literal you can use " to enclose the literal. Now in VB to include a " in string literal you double them to "".

BTW, don't "quote" me (sorry) but in javascript I think you can get away with the trailing comma.

Upvotes: 2

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26727

you have to escape the single quote

...<%="\'" & trim(Mid(cartobj(intA).Item("ItemNo"),4)) & "\',"%><%Next%>];

Upvotes: 1

Related Questions