0

I'm trying to seperate a list with a line break but neither "\n" nor a br tag are working. Anybody know why?

var c_list=['One','Two','Three']
for (var x in c_list){
    holder_string += c_list[counter]
    holder_string += "\n"
    counter++
    $("#list").text(holder_string);
}

1 Answer 1

4

\n isn't really showed in the browser, other than in the source, and <br /> doesn't work in text() as it doesn't accept html, html() does

var c_list=['One','Two','Three']
for (var x in c_list){
    holder_string += c_list[counter]
    holder_string += "<br />"
    counter++
    $("#list").html(holder_string);
}

Note that you could just do

$("#list").html( c_list.join('<br />') );
Sign up to request clarification or add additional context in comments.

3 Comments

dat answer speed... +1
\n shows up if the HTML element is white-space: pre -- which it probably isn't, given OP's issues.
@Blazemonger - Yeah, it also shows up inside <pre> tags and in a few other edge cases, but generally newlines and whitespace between tags makes no difference to layout.

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.