i have a
var ID= "12,32,23,78";
var i = ID.split(',');
if i do this then work fine but when it is only 12 then he give me 0. how i can solve this issue if i need to check if it have then how
i have a
var ID= "12,32,23,78";
var i = ID.split(',');
if i do this then work fine but when it is only 12 then he give me 0. how i can solve this issue if i need to check if it have then how
Exactly like what you posted except you check for the presence of the seperator with JavaScripts .indexOf() string method.
String.prototype.mySplit = function(sep) {
return (this.indexOf(sep) != -1) ? this.split(sep) : [this];
};
Example:
//var ID= '12,32,23,78';
var ID= '12';
//Update
if (typeof(ID)=='number') ID += '';
var i = ID.mySplit(',');
alert(i[0]);
var ID= "12,32,23,78";
var i = ID.split(',');
will return [12,32,23,78]
var ID= "12";
var i = ID.split(',');
will return [12] -- this is also an array
however you may do this
var ID= "12";
var i = ID.split(',') || ID;