1

So I'm doing a quote generator in Javascript in which the quote generates on click.

The code looks like this:

$("#getQuote").on("click", function(){
var i = Math.floor((Math.random() * quote.length));

   $("#theQuote").html(quote[i] <\br> person[i]);
});

I'm trying to make a line break between the quote and the person who made the quote but it doesn't seem the recognize any code after quote[i].

I've also tried adding \n to the strings in my array for example:

var quote = ['"With great power, comes great responsibility." \n- Uncle Ben'];

but somehow \n is not read whereas the rest of the quote is.

2 Answers2

2

1st $("#theQuote").html(quote[0] + '<br>' + person[0]); anything without wrap by ' or " are either number or variable, and .html take string as html, so you need concate them use +

2nd this way will also work, var quote = ['"With great power, comes great responsibility." <br> - TEST_1 Uncle Ben'];

var quote = ['"With great power, comes great responsibility." <br> - TEST_1 Uncle Ben'];
var person = ['TEST_2 Uncle Ben'];

$("#getQuote").on("click", function() {
  //var i = Math.floor((Math.random() * quote.length));

  $("#theQuote").html(quote[0] + '<br>' + person[0]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button id="getQuote">getQuote</button>

<br><br>
<div id="theQuote">N/A</div>
Dalin Huang
  • 11,212
  • 5
  • 32
  • 49
  • I somehow thought .html passed html code. Thank you for clarifying that it's in the form of strings and requires inverted commas. – confused.coder Jun 07 '17 at 11:58
-1

Change <\br> to <br> or <br />. This is according to the HTML5 standards.