Home > Back-end >  How to properly pass variables in an array from a form using Javascript jquery
How to properly pass variables in an array from a form using Javascript jquery

Time:10-13

when I run the code below it works fine

<script>

var key_values =[79, 129, 183, 128, 66];
const secret_keys = new Uint8Array(key_values);
console.log(secret_keys);

//Uint8Array(64) [ 79, 129, 183, 128, 66 ]

</script>

Here is my issue:

when I pass the key_values variable into an arrays from form inputs. it does not display any value in the console.

This is what it displayed in the console Uint8Array [0] instead of Uint8Array(64) [ 79, 129, 183, 128, 66 ]

here is the code.

<script>
            $(function () {
                $('#save').click(function () {
                    
// inputted form values 79, 129, 183, 128, 66

var key_values = $('#my_keys').val();

const secret_keys = new Uint8Array([key_values]);
console.log(secret_keys);

//Uint8Array [0]



    });
            });
</script>
<input type="text" id="my_keys" value="79, 129, 183, 128, 66">

                    <input type="button" id="save" value="save" />

CodePudding user response:

You need to parse the string as an array before passing it to the constructor or just split by comma to get an array.

const secret_keys = new Uint8Array(key_values.split(","));
  • Related