开发者

jquery or javascript array appending to div as a text

开发者 https://www.devze.com 2023-02-28 10:44 出处:网络
my problem is with appending a array into existing div as a text. I cant figure it out why its not working, so i hav开发者_JAVA技巧e this code:

my problem is with appending a array into existing div as a text. I cant figure it out why its not working, so i hav开发者_JAVA技巧e 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)


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


you can do something like this to explicitly convert array to string.

$('#result2').append(array1+'');

Here's a working example

http://jsfiddle.net/EsnLs/2/

0

精彩评论

暂无评论...
验证码 换一张
取 消