Nave Tseva
Nave Tseva

Reputation: 401

making new line in JS

Hi I have this JS code

  function myFunction() {
  var g; 
var d = new Date().getDay();
switch (d) {
    case 0:
        g = "Today it's Sunday";
        break;
    case 1:
        g = "Today it's Monday";
        break;
    case 2:
        g = "Today it's Tuesday";
        break;
    case 3:
        g = "Today it's Wednesday";
        break;
    case 4:
        g = "Today it's Thursday";
        break;
    case 5:
        g = "Today it's Friday";
        break;
    case 6:
        g = "Today it's Saturday";
        break;
    default:
        x = "Looking forward to the Weekend";
}


var y; //popup

var name = prompt("Please enter your name", " (your name is here)");

if (name != null) {
    y = 'How are you today?';
    document.getElementById("popup").innerHTML = y; //
}
var x = ""; //שעה
var time = new Date().getHours();
if (time < 12)  //תנאי
{
    x = "Have a beautiful morning " + name + '! ';
}
else if (time < 16) {
    x = "Great noon! Enjoy this day " + name + '!';
}
else if (time < 19) {
    x = "Great afternoon! Enjoy them " + name + '!';
}
else if (time < 23) {
    x = "Good evening! have some fun and take some rest befor the day over" + name + '!';
}

document.getElementById("demo").innerText = x + "<br/>" + g; 
}

why whan I put the br tag in its just writs the br tag on the screen and there is now break between the line? I alsow tried to put the "\n" and it doesnt work too. why?

Upvotes: 3

Views: 738

Answers (3)

Alexander Pavlov
Alexander Pavlov

Reputation: 32306

You need to set innerHTML rather than innerText. However beware of different kinds of security attacks and sanitize the user-provided text (i.e. name).

Upvotes: 3

Bergi
Bergi

Reputation: 665486

To make the <br/> tag become an element, you'd need to set the innerHTML instead of innerText.

The \n approach would have also worked. Only, whitespaces don't appear in the layout usually. If you set the CSS style of the #demo element to white-space:pre-wrap;, the linebreak will show up.

Upvotes: 1

lebhero
lebhero

Reputation: 1441

you are setting the innerText which will only set the Text of the Element. Try :

document.getElementById("demo").innerHTML = x + "<br/>" + g;  

Upvotes: 1

Related Questions