0

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>"; 
Martin Dev
  • 67
  • 2
  • 8

1 Answers1

1

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

Community
  • 1
  • 1
NS0
  • 6,016
  • 1
  • 15
  • 14