Umer Umer - 1 month ago 11
Javascript Question

Divide the anchor string value?

I have an anchor which I inject in HTML in jqGrid formatter as below:

var number = rowObject.number;
var plateNumber = rowObject.plateNmber;
var markup = "<a href=%Href%;>%Text%</a>"
var replacements = {
"%Text%": plateNumber ,
"%Href%": "javascript:Search.openViewByPlateNumber(" + number + "," + plateNumber + ")"
};

markup = markup.replace(/%\w+%/g, function(all) {
return replacements[all];
});


Here is my
OpenViewByPlateNumber
function:

var OpenViewByPlateNumber = function(number, plateNumber) {
// Do something
};


In the UI there will be a number in the grid. When I click on the number the
openViewByPlateNumber
function will be called. Everything is working fine for me. The problem is the plate number is a string type. It is a number but it can be
1
,
2
,
3/4
,
340/2
etc.

It's working fine when number is simple like
1
,
5
or
9
but if number is
340/2
, then the method receives a value of
170.5
. It divides the number. So how I can pass it as string?

Answer

To pass the values to the function as a string wrap them in quotes:

"%Href%": 'javascript:Search.openViewByPlateNumber("' + number + '","' + plateNumber + '")'