isaac weathers
isaac weathers

Reputation: 1472

Put line break in text string within a variable array in JS/jQuery

Noob Question:

I have a simple switch for some static text that is set to switch at a certain interval and for some reason I cannot get the line break to work within the string. I've tried a few different methods of using \n or
and even tried encoding and it won't break the string into two sentences. Anyone see what I'm doing wrong? The text change is working. I am trying to get the string to break into a new line where the "\n" is located.

Here's a working fiddle:

JS Fiddle

Script:

   var texts = ["Blah Blah"+"\n"+"Blah Blah Line two", "Over 15,000 cups of"+"\n"+"Starbucks drank daily!", "Another awesome%0D%0Aslogan here"];
var count = 0;
function changeText() {
    $("#example").text(texts[count]);
    count < 3 ? count++ : count = 0;
}
setInterval(changeText, 500);

Html:

<div id="Div-A"><h2><span id="example">Over 15,000 eligible<br/>dealers nationwide</span></h2></div>

Upvotes: 1

Views: 3124

Answers (1)

ntgCleaner
ntgCleaner

Reputation: 5985

You are using .text() to input your slogans.

I changed your .text() to .html() and just put simple line breaks in your sentences:

$(document).ready(function() {
    var texts = ["Over 5,000 beers <br /> drank today", "Over 15,000 cups of <br /> Starbucks drank daily!", "Another awesome<br />slogan here"];
    var count = 0;
    function changeText() {
        $("#example").html(texts[count]);
        count < 3 ? count++ : count = 0;
    }
    setInterval(changeText, 500);
});

http://jsfiddle.net/2dBVM/2/

Upvotes: 3

Related Questions