javascript problems with array

71 Views Asked by At

i've to write something like this:

document.getElementsByName("elName[]")[0].value=Array('val1','val2','val3', etc);

My problem is inside the "Array" elements, because i have to write inside some values from an array already initialized

var val_vec= new Array(<?php echo $count ?>);

i've tried something like this, but it doesn't work:

document.getElementsByName("elName")[0].value=Array(
            for(var i=0; i< <?php echo $count ?> ; i++)
            {
                document.write('"'+ val_vec[i] + '"');
                if(i!=<?php echo $count-1 ?>)
                    document.write(',');
            });

the 'if' condition it is writed because of all commas betweens 'vars' (the last comma must not be written!)

Can anyone help me please?

1

There are 1 best solutions below

1
On

Your syntax makes no sense whatsoever. Using a for loop inside of Array(…), that is just complete nonsense.

If you want to get all array values as a comma-separated string, then simply use .join()

Or, if your question is about how to get a PHP array of values “transferred” to a JavaScript variable (really hard to tell what you actually want to know) – use PHP’s json_encode.