1

everyone!

I'm writing a trivia game for a website with JavaScript that stores all question texts, answers, and level of difficulty in a single, multi-dimensional array and sometimes I need to put book titles in italics. However, I'm having trouble with the italics.

I'd love to just use HTML tags to do this but they just print on the screen like this:

The title of the books is <i>I Wish I were in Italics!</i>

I also tried making each title into a string variable and using the String italics() method like this:

var italicsTest = "I Wish I were in Italics!";    
Question = ["1", "2", "1824", "The title is" + italicsTest.italics()];

That produces the same result as above. The HTML tags show up as text on the page.

Does anyone know how I can fix this? Thanks!

-ThatsIsJustCrazy

** EDIT: I was inserting the text into my HTML file with JQuery like this:

$(".questionText").text(Question[randomNum1][randomNum2]);

but I should have been using "html" not "text." It needed to be this:

$(".questionText").html(Question[randomNum1][randomNum2]);

Thanks, everyone!

  • 1
    How you actually put the text to the page? Also, [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/italics) says, that `String.prototype.italics()` is deprecated. You can't rely on it working in all browsers. – Teemu Dec 12 '15 at 22:50
  • I have a paragraph tag in my HTML page like this:

    Temporary Text

    I replace that text in my javaScript files using JQuery with this: $(".questionText").text.(ArrayName[randomNumber1][randomNumber2]);
    – That'sIs JustCrazy Dec 12 '15 at 22:52
  • If you don't insert your text as html, then it won't get rendered as html :). Paste the snippet that inserts your question into the html page. – gion_13 Dec 12 '15 at 22:52
  • Please edit your post for additional information. – Teemu Dec 12 '15 at 22:53
  • Can you tell me how to insert the text as HTML? – That'sIs JustCrazy Dec 12 '15 at 22:58
  • Change `.text(ArrayName[randomNumber])` to `.html(ArrayName[randomNumber])` - in other words, use `.html()` instead of `.text()`. – Pointy Dec 12 '15 at 22:58
  • Wow! Thanks!! Do you know if ,html() has any browser compatibility issues? – That'sIs JustCrazy Dec 12 '15 at 23:00
  • @That'sIsJustCrazy no, it'll work in any browser made since the late 1990s. – Pointy Dec 12 '15 at 23:06
  • Perfect! Thanks so much Teemu, gion_13, and Pointy! – That'sIs JustCrazy Dec 12 '15 at 23:08

1 Answers1

2

Try this:

var italicsTest = "I Wish I were in Italics!"; 
var Question = ["1", "2", "1824", "The title is " + italicsTest.italics()];

$(document).ready(function() {
  $.each(Question, function(index){  
   $("<p>"+Question[index]+"</p>").appendTo($('#content'));
 });
});

on jsfiddle

or with html() on jsfidlle