0

My array looks like

["John Connor ", "Mike ", "Ryan Jones ", "Markey O ", "Markey B"]

I'm trying to put these into multiple strings (though splitting into multiple strings may not be the best way) so I can place them on the page one below the other.

So I do $(".info_container").text(myArray);

How can I split these at the , and then place them into the DOM?

I tried $(".info_container").text(myArray).join(","); but this still just gives me a string of arrays not individual strings.

asked Mar 3, 2016 at 10:25
3

3 Answers 3

2

Try:

var array = ["John Connor ", "Mike ", "Ryan Jones ", "Markey O ", "Markey B"];
array.forEach(function(name) {
 $(".info_container").append('<span>' + name + '</span>');
});
answered Mar 3, 2016 at 10:28
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers man! This worked. I didn't really think of doing a forEach over my array :)
2

You can use .innerHTML with arrary.join(', <br>'):

var arr = ["John Connor ", "Mike ", "Ryan Jones ", "Markey O ", "Markey B"];
document.body.innerHTML = arr.join(', <br>');

answered Mar 3, 2016 at 10:35

Comments

1

You can try this:

var myArray = ["John Connor ", "Mike ", "Ryan Jones ", "Markey O ", "Markey B"].toString();

Then:

$(".info_container").text(myArray);
answered Mar 3, 2016 at 10:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.