0

I have an application which uses MVC architecture, the price of an entity is returned in a string in HTML format for INR currency as the currency symbol of INR is not in ASCII char set. I have to use this recieved price in the javascript as described below

var someVariableWhichIWantNotToBeInHTML = "${viewModel.price}"

which converts to

var someVariableWhichIWantNotToBeInHTML = "<span class = "currencyINR"></span>1.00"

but it is leading to syntax error as " is not closed in closing span tag.

Can you please suggest me how to use variables in JS that can have such HTML in them coming from the viewmodel?

user3166642
  • 105
  • 1
  • 7
  • [Custom font](http://stackoverflow.com/questions/12144000/using-custom-fonts-using-css)? – hindmost Jan 09 '17 at 19:04
  • The main problem is that this price is getting populated in the view model using an external API.I cannot change the format in which they are sending the strings.Is there anyway to read price from such strings. Even if I write a custom function which will remove all the HTML content of the string and keep only price it will still result in syntax error. – user3166642 Jan 09 '17 at 19:10
  • Frankly I don't see what is the problem. You just have to define specific CSS class. You can't edit CSS on your page? – hindmost Jan 09 '17 at 19:17
  • There is no such thing called formatted currency variables in JS. – varunsinghal65 Jan 09 '17 at 19:18
  • formatted as in the currency symbol will be there in the string.[Updating the question with clarification] – user3166642 Jan 09 '17 at 19:19
  • `var varName = "1.00"` causes a syntax error, you've to wrap the value string with single quotes, or escape the inner quotes with a backslash. – Teemu Jan 09 '17 at 19:40
  • Of what relevance is ASCII to HTML? HTML and Javascript and JSP characters are Unicode. So, you can use the ₹ sign. – Tom Blodget Jan 10 '17 at 02:56

1 Answers1

3

Assuming you can modify the values of ${viewModel.price}. You have two options :

Option 1 :

Escape the double quotes like this :

  "<span class =\"currencyINR\"></span>1.00"

Option 2 :

enclose the currency INR within single qoutes :

"<span class = 'currencyINR'></span>1.00"

Usually the API should handle this, but since it doesn't following should do the trick :

var someVariableWhichIWantNotToBeInHTML = '${viewModel.price}'

Through this you will get the string without error then you can extract price through a custom function and use CSS to display INR sign.

varunsinghal65
  • 536
  • 1
  • 5
  • 20
  • I want one clarification in the answer.How does "${viewModel.price}" and '${viewModel.price}' differ (one is with single quote another is with double quotes) – user3166642 Jan 09 '17 at 19:49
  • 1
    In case of single qoutes Js Interpreter isnt confused as it can find a pair of double qoutes within a pair of single qoutes but in case of double qoutes it gets confused . As it is unable to determine the matching double qoute – varunsinghal65 Jan 09 '17 at 20:25
  • Thanks, looks like a hacky way but satisfies my use case. – user3166642 Jan 09 '17 at 21:04
  • This is a hacky way, ideally API should take care of this. Moreover API should return only price i dont understand why it is returning HTML – varunsinghal65 Jan 10 '17 at 03:51
  • If it helped you, could you please accpept the answer? – varunsinghal65 Jan 27 '17 at 21:26