3

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)

1
  • BTW you don't have to initialize arra1 with an empty array. You could even write e.g.: var new_tor = $.unique(tor.split(' ')).join(' '); Commented Apr 23, 2011 at 8:03

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

2

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/

1 Comment

Just a note : array1 + '' works because JS is trying to concatenate '' with the array thereby calling .toString() automatically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.