Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery or javascript array appending to div as a text

my problem is with appending a array into existing div as a text. I cant figure it out why its not working, so i have this code:

var raya = ui.item.value + ' ';  
$('#result').append(raya);
var tor = $("#result").text();

Above code is working, the value of raya (which is string) is appended correctly into #result
Problem comes here, the value of array1 is not appended to #result2 and ideas why its not working?

var array1 = new Array();
array1 = tor.split( " " );
array1 = $.unique(array1);
$('#result2').append(array1);
return false;

(just to mention that everything is holded in 1 function, this cant be the reason, but just to know)

like image 797
hayllone Avatar asked Jan 24 '26 08:01

hayllone


1 Answers

That's because append expects a string and you're sending it an array.

Two ways to solve it. Use either .toString() or .join()

$('#result2').append(array1.toString()); //inserts a comma separated list
$('#result2').append(array1.join(' '));  //inserts a string separated by what you pass as param
like image 180
JohnP Avatar answered Jan 26 '26 21:01

JohnP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!