Home > Blockchain >  Assign value (array) to a value of a variable in Jquery
Assign value (array) to a value of a variable in Jquery

Time:10-29

I have an array called comerciosAMostrar , and a selector what is taking the value of it called $('#numeroComercioPillbox').val(), i just want to assign the array to the value, but it's throwing me an error like

Invalid left-hand side in assignment

Do you know why?

$('#numeroComercioPillbox').val() = comerciosAMostrar

CodePudding user response:

$('#numeroComercioPillbox') what element is this? , and what is the output you need to display? please provide more information.

CodePudding user response:

What you're trying to do is assigning an array to a function call.

The right syntax would be -

$('#numeroComercioPillbox').val(<some-value>);

<some-value> should be a string.

Since you're trying to assign an array, it might be better to JSON.stringify() it as -

$('#numeroComercioPillbox').val(JSON.stringify(comerciosAMostrar));

Update: Instead of JSON.stringify(), you could also .join() as Rory in the comments suggested.

$('#numeroComercioPillbox').val(comerciosAMostrar.join(', '));
  • Related