Martin Dev
Martin Dev

Reputation: 67

jQ Format is not a Function

I'm having this error when on this line when I'm formatting a string using jQuery, Uncaught TypeError: f.format is not a function

This is the line where the error seems to be appearing (I've got multiple lines using the same method but none of these seem to be working)

var $li = $(f.format(betid, bet.amount, bet.icon, bet.name, bet.amount));

Why is this happening? I've used this before and it worked just fine?

String:

var f = "<div>";
f += "<div class='avatar''>";
f += "<img src='{2}'>";
f += "</div>";
f += "<div>{3}</div>";
var num = bet.amount;
f += "<div class='user-bet ng-binding'>" + num + "</div>";
f += "</div></div>"; 

Upvotes: 0

Views: 4658

Answers (1)

NS0
NS0

Reputation: 6106

string.format is not a function in Javascript

You can write such a function however:

String.prototype.format = function() {
  var str = this;
  for (var i = 0; i < arguments.length; i++) {       
    var reg = new RegExp("\\{" + i + "\\}", "gm");             
    str = str.replace(reg, arguments[i]);
  }
  return str;
}

From this question

Upvotes: 1

Related Questions