Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript script.js line break in a variable quotes = [" (no solution)

yulB

New Coder
Hi, sunny today by -19celcius. I come to warm up here with all your sunshine 🙂
(I'm not familiar with forums) I'm really a beginner (and old ha ha ha)
I found some programming that allows me to display random sentences from HTML. The code is fine but after some reflections and adaptations I am trying to make a line BREAK in a variable so that the name of the creator of the quote is after the sentence, below.

For example and simplified, a sentence:
"So shaken as we are, so wan with care, Henry announces to his court. Shakespeare"
I would like this:
"So shaken as we are, so wan with care, Henry announces to his court." (and on a return line the rest of the sentence!)
"Shakespeare"

I can't find...
Question 1)
The code used (which I did not create comes from a codePen site. The script.js gives this and works (in short) but not possible to find a line break

JavaScript:
var quotes = ["So shaken as we are, so wan with care, Henry announces to his court. Shakespeare",
or something like "So shaken as we are, so wan with care, Henry announces to his court." + "Shakespeare" (i try a \n or <br> dont work!?)
"To be, or not to be: that is the question.  Shakespeare"
]; etc. etc.

function getQuote() {
var randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
document.getElementById("parag").innerHTML="<em>" + randomQuote + "</em>";
}

Question 2) How to add a new image with this button at each click
Thanks, i appreciate if you can direct me to a website or a way to do it!
 
Solution
Hi there.

"\n" is not a line break in HTML. It can work with some browsers and cases, but in any means do not trust on it.

You should include the "<br/>" in your string directly. Your way makes it easy for errors.

"<br>" and "<br/>" both works with modern browsers, but "<br/>" is gerenally preferred over "<br>"

JavaScript:
// not tested, but should work.

var quotes = [
    "So shaken as we are, so wan with care, Henry announces to his court.<br/><em>Shakespeare</em>",
    "To be, or not to be: that is the question.<br/><em>Shakespeare</em>"
    // more quotes here
];

// get random quote
function getQuote() {
    var randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
    // Use innerHTML to allow HTML tags like <br/> render...
Hi there.

"\n" is not a line break in HTML. It can work with some browsers and cases, but in any means do not trust on it.

You should include the "<br/>" in your string directly. Your way makes it easy for errors.

"<br>" and "<br/>" both works with modern browsers, but "<br/>" is gerenally preferred over "<br>"

JavaScript:
// not tested, but should work.

var quotes = [
    "So shaken as we are, so wan with care, Henry announces to his court.<br/><em>Shakespeare</em>",
    "To be, or not to be: that is the question.<br/><em>Shakespeare</em>"
    // more quotes here
];

// get random quote
function getQuote() {
    var randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
    // Use innerHTML to allow HTML tags like <br/> render
    document.getElementById("parag").innerHTML = randomQuote;
}

If you have both ways to make a line break in your data, then you can use simple function to edit "\n" to "<br/">.

JavaScript:
randomQuote = randomQuote.replace(/\n/g, "<br/>");
 
Last edited:
Solution

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom