0

In the body, it can't present the variable value, but only shows variable name "var1". Besides, I use "\n" in document.write, but how come it doesn't break the line in the result?

    <script type="text/javascript">
        var var1=123;
        document.write("<strong>Hello World! \nThis is the second line.</strong>");
    </script>
</head>
<body>
    <h1>the value for number is:  + var1</h1>
</body>

random_user_name
  • 25,694
  • 7
  • 76
  • 115
Cathy
  • 37
  • 1
  • 4
  • You can't display a variable like that. However, you can create a new element with tag `h1` and format the value of your variable in. – e.doroskevic Nov 23 '18 at 23:48
  • `\n` aka, "line feed", doesn't show in HTML, unless it's within `
    ` tags*.  If you want it on a separate line, then you need to use a `
    ` tag, or wrap the line(s) in `

    ` tags. *_technically, within any element styled to have `white-space: pre`_

    – random_user_name Nov 24 '18 at 00:00
  • 1
    Possible duplicate of [how to display a javascript var in html body](https://stackoverflow.com/questions/40858456/how-to-display-a-javascript-var-in-html-body) – random_user_name Nov 24 '18 at 00:02
  • Please read [ask], and pay special attention to the part about _only asking one question at a time_. – random_user_name Nov 24 '18 at 00:06

3 Answers3

0

Here is a basic example.

const name = 'john doe'; 
const header = document.createElement('h1');
const text = document.createTextNode(`my name is ${name}`); 

header.appendChild(text);
document.body.appendChild(header);
e.doroskevic
  • 2,129
  • 18
  • 25
  • More practical way to do it is using textContent property of HTML nodes. `const myDiv = document.querySelector('#some-id');` `const myVariable = 'Some data about something';` `myDiv.textContent = myVariable;` if you'd like to concatenate to the existing string `myDiv.textContent = myDiv.textContent + ' Some more string';` – Ertan Kara Nov 23 '18 at 23:57
  • Might be better to mark this question as a duplicate (which it is), rather than offering a new answer :) To understand why, maybe check this out: https://meta.stackexchange.com/a/10844/178653 – random_user_name Nov 24 '18 at 00:03
  • @cale_b i agree :) – e.doroskevic Nov 24 '18 at 00:06
0

Use the line-break tag, br, instead of \n.

As for the displaying the variable:

The variable var1 can only be used inside the javascript code block. The line

<h1>the value for number is:  + var1</h1>

will not be executed as actual javascript code since it is not inside the javascript block. You can do it like this:

<head />
    <script>
        function main() {
            var var1=123;
            document.getElementById("text").innerHTML += var1
        }
        document.write("<strong>Hello World! <br />This is the second line.</strong>");
    </script>
</head>
<body onLoad="main()">
    <h1 id="text">the value for number is: </h1>
</body>
Viktor W
  • 1,139
  • 6
  • 9
0

As for the variable part of the question, there are at least 2 ways of achieving what you want:

Method 1

Use DOM Manipulation to change the inner text of the h1 tag like so:

var var1=123;
document.getElementsByTagName('h1')[0].innerText = "the value for number is: " + var1

Method 2

You can write html in EJS(Embedded Javascript) format, where in essence you can pass variables directly to the html tags:

<% var var1=123; %>
<h1>the value for number is: <%= var1 %></h1>

If you are interested in learning about EJS then look here. It's worth mentioning that EJS is an external library (thanks cale_b for pointing this out in the comments)

As for the break part of the question, you need to replace "n\":

document.write("<strong>Hello World! <br/>This is the second line.</strong>");
Devtician
  • 81
  • 8
  • It's worth mentioning that [ejs](https://ejs.co/#install) is a _library_, may not be worth the overhead to bring in a library. If OP is going to do a _lot_ of this, then it may make sense - but IMO there's usually a lighter-weight way to solve problems like this. – random_user_name Nov 24 '18 at 00:05
  • @cale_b I agree, it does require installing an external library, that's why I have provided 2 methods of solving this problem, where method 1 does not require external libraries, and method 2 does. This way the OP can choose, which method is more suitable in his/her situation. – Devtician Nov 24 '18 at 00:09