wyc
wyc

Reputation: 55283

How to add a dollar sign to this output (jQuery)?

The following code outputs the prices of some items:

var $dishPrice = $("<span>").addClass("dish-price").text(topDish.price || '').appendTo($header);

I would like to add a dollar sign right before each number. What's the best way of doing this?

Upvotes: 1

Views: 5994

Answers (3)

Dan
Dan

Reputation: 2341

If you have multiple currencies, I would say doing it in CSS might be a simpler solution. Reason for that is the fact that some currency are displayed on the left side of the price, while others on the right side.

So simply add the currency type as a class name.

JavaScript:

var $dishPrice = $("<span>").addClass("dish-price").addClass("usd").text(topDish.price || '').appendTo($header);

CSS:

.dish-price.usd:before{content:'$'}
/* For example Swiss Francs goes to the right side */
.dish-price.chf:after{content:' franc'}

Upvotes: 3

Sarfraz
Sarfraz

Reputation: 382726

Add '$' + before your output :

var $dishPrice = $("<span>").addClass("dish-price")
.text('$' + topDish.price || '').appendTo($header);

In JS, + is not only used to add numbers but also concatenation of strings.

Upvotes: 7

giorgio
giorgio

Reputation: 10202

var $dishPrice = $("<span>").addClass("dish-price").text('$' + topDish.price || '').appendTo($header);

like this?

Upvotes: 2

Related Questions