user1507789
user1507789

Reputation: 21

Creating a Java Script Array from hidden values

var a1=$("#orderprogress").val().toFixed(2);//a1=50
var a2=$("#poprogress").val().toFixed(2); //a2=70

if i want to create a array like this how would i do it?

graphData = new Array(
             [a1 value,'#222222'],//[50,'#222222']
             [a2 value ,'#7D252B']//[70,'#222222']
        );

Upvotes: 1

Views: 111

Answers (4)

Ian
Ian

Reputation: 50905

Try using the following code:

var a1 = +(+$("#orderprogress").val()).toFixed(2);
var a2 = +(+$("#poprogress").val()).toFixed(2);

graphData = [
    [a1, '#222222'],//[50,'#222222']
    [a2, '#7D252B']//[70,'#222222']
];

DEMO: http://jsfiddle.net/ERccS/4/

This will take the textbox value (a string), convert it to a number, call toFixed(2) on it, then convert it back to a number.

Unfortunately (if you care), "50" will be displayed as 50 (this happens with trailing 0s). If you always need 2 decimal places no matter what, take off the first + I have in my code - they will be kept as strings and always have 2 decimal places.

I'm not exactly sure what you're looking to do with the toFixed. The .val() method always returns a string. toFixed isn't a String method - it's a Number method. And its result is the original Number rounded to a certain number of decimal places (what you pass to the method). In your case, it's 2.

Upvotes: 4

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

try this,

var xArray=[];

var a1=$("#orderprogress").val().toFixed(2);//a1=50
var a2=$("#poprogress").val().toFixed(2); //a2=70

InsertIntoArray(a1,'#fff')
InsertIntoArray(a2,'#fff')

function InsertIntoArray(x,y)
{
var yArray=[x,y];

xArray.push(yArray);

}

or simply,

   var xArray=[];

    var a1=$("#orderprogress").val().toFixed(2);//a1=50
    var a2=$("#poprogress").val().toFixed(2); //a2=70

    xArray.push([a1,"#fff"],[a2,"#fff"]);

Upvotes: 0

Jazzepi
Jazzepi

Reputation: 5480

Either

var graphData = new Array(new Array(a1,'#222222'),new Array(a2 ,'#7D252B'));

OR

var graphData = [[a1,'#222222'],[a2,'#7D252B']];

Upvotes: 2

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

graphData = [[a1, '#222222'], [a2, '#7D252B']];

Upvotes: 2

Related Questions