Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Unruly array not staying in containing div

Unruly array not staying in containing div

Problem

On my website I have a JS function that produces an Array that can be of varying size depending on what input is given by the use

This is what I use to display the output array, perArrary, and some text.

document.getElementById("happyanswer").innerHTML= perArray + " are so happy, yes they are!!!";

The part in quotes itself always stays in the div . When it displays the array's Elements it pushes to the right until it displays all elements.

I have the fiddle here http://jsfiddle.net/m57KC/

Right now the only button that works is the happy numbers button. You can also ignore whatever issues there still are other than this one( there are many I know).

Problem courtesy of: Chris Jones

Solution

The problem is that you are converting an array directly to a string, with no .join(). By default, javascript joins the elements with just a comma, like 109,103,100,97,94,91,86,82,79,70,68,49,44,32,31,28,23,19,13,10,7,1 are so happy, yes they are!!!. Commas don't trigger line-wrapping. If you join with a comma and space, instead, the line will wrap automatically:

document.getElementById("happyanswer").innerHTML= perArray.join(', ') + " are so happy, yes they are!!!";
Solution courtesy of: Ed Cottrell

Discussion

View additional discussion.



This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

Unruly array not staying in containing div

×

Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription

×