Span in prompt

<!DOCTYPE html>
<html>
<body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction()
{
var x;

var nr=prompt("Please enter number","number1");
var nr2=prompt("Please enter number","number2");




  
  x=parseInt(nr)+parseInt(nr2)+<span style="color:blue">nr-nr2</span> 

  document.getElementById("demo").innerHTML=x;
  
}
</script>

</body>
</html>

Please, what is wrong here?
I just want to get second number couored blue…
Can someone help me?
Many thanks!

Hi there,

The problem is this line:

x=parseInt(nr)+parseInt(nr2)+<span style="color:blue">nr-nr2</span>

You cant’ just add HTML to a calculation like that.

Here’s a better way:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Colour me blue</title>
    <style>p#result{ color: blue; }</style>
  </head>
  
  <body>
    <p>Click the button to demonstrate the prompt box.</p>
    <button id="myButton">Try it</button>
    <p id="result"></p>

    <script>
      function addTwoNumbers(){
        var nr1 = prompt("Please enter number", "number1");
        var nr2 = prompt("Please enter a second number", "number2");
        var result = Number(nr1) + Number(nr2);
        document.getElementById("result").innerHTML = result;
      }

      var button = document.getElementById("myButton");
      button.addEventListener('click', addTwoNumbers, false);
    </script>
  </body>
</html>

Notice the separation of HTML, CSS and JS.
If you can get used to doing this from the start, this will help you write better code.

If you have any questions, just ask.

Hello!
Please, I want just the second result (nr1-nr2) to be blue, and the first result to be red!

And, I would like to get each result in its own line!
Please, can somone help?
Many thanks!

Please, here is the code:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction()
{
var x;

var nr1=prompt("Please enter nr1","number1");
var nr2=prompt("Please enter nr2","number2");



  x=parseInt(nr1) + parseInt(nr2)+'\
'+nr1-nr2;
  document.getElementById("demo").innerHTML=x;

}
</script>

</body>
</html>

Many thanks!

Is this any help?

<!DOCTYPE html>
<html>
<body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction()
{
var x;

var person=prompt("Please enter\\r\
your name","Harry Potter");

if (person!=null)
  {
  x="Hello " + person + "!<br>How are you today?";
  document.getElementById("demo").innerHTML=x;
  }
}
</script>

</body>
</html>

Hi mkjs,

Is there any reason you didn’t keep the code Pullo supplied? Or, at least follow his advice

You cant’ just add HTML to a calculation like that
?

This:

x=parseInt(nr1) + parseInt(nr2)+'\
'+nr1-nr2;

is like a run-on sentence in English.

  1. you need at least 2 statements, not one.
  2. "
    " is meaningless for ‘innerHTML’. You need “<br />”

So, you would need to do something like:


x=parseInt(nr1) + parseInt(nr2);
y =nr1-nr2;
document.getElementById("demo").innerHTML= x + "</br>" + y;

Insofar as the coloring, look at what Pullo did & expand upon it. Hint: you can use more than 1 paragraph or 2 spans, or 2 divs…dependent upon whether this a homework assignment or a learning experience.

Vinny

Thanks, all, but I work at momoent in w3schools online and it does not work.
I get prompts to imput noumbers, after that I just don’t get any resutls.

<snip>

Please, help!
Here is the code:

<!DOCTYPE html>
<html>
 <head>
    <meta charset="utf-8">
    <title>Colour me blue</title>
    <style>p#x{ color: blue; font-weight:bold }</style>
   
  </head>
  



<body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>

<p id="x"></p>


<script>
function myFunction()
{
var x;

var nr1=prompt("Please enter nr1","nr1");
var nr2=prompt("Please enter nr2","nr2");


  
  x=parseInt(nr1)+parseInt(nr2);
  y=nr1-nr2;
  document.getElementById("x").innerHTML=x+"<br>"+y;
 
  
}
</script>

</body>
</html>

Many thanks!

Hi there,

Two things.

  1. Please use code tags when you post code. Here’s how.
  2. Please don’t start multiple threads for the same problem.

Hello!
This is mz atempt to write out each result inother color.
Plese do u see wht is wrong_
Many thanks!!!

<!DOCTYPE html>
<html>


<head>
    <meta charset="utf-8">
    <title>Colour me blue</title>
    <style>p#x{ color: blue; font-weight:bold }
p#y{ color: red; font-weight:bold }
</style>

  </head>

<body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>

<p id="x"></p>
//<p id="y"></p>


<script>
function myFunction()
{
var x;

var nr1=prompt("Please enter number","number1");
var nr2=prompt("Please enter number","number2");





  x=parseInt(nr)+parseInt(nr2);
y=nr1-nr2;

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

}
</script>

</body>

Just one thing to consider is that the Netscape browsers that required the use of prompt() do not support innerHTML.

Those browsers that do support innerHTML provide far superior replacements for prompt() as well - which is why prompt() is never taught any more in proper programming classes (only in “history of JavaScript” classes).

Hi there,

Ok, let’s start over :slight_smile:

Let’s take a skeleton HTML document:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <style></style>
  </head>
  
  <body>
    
    <script>
    </script>
  </body>
</html>

Notice the correct position of the <script> tags, just before the closing </body> tag.

The first thing you are trying to do is have a button and make it do something when you click it.
You shouldn’t do this with inline JavaScript, rather by using the addEventListener method.

element.addEventListener(<event-name>, <callback>, <use-capture>);

You should name your callback something useful. “myFunction” is not a good name, as it gives no indication as to what the function is doing.
This gives us this:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <style></style>
  </head>
  
  <body>
    <button id="myButton">Try it</button>

    <script>
      function addTwoNumbers(){
        // Do your arithmetic here
      }

      var button = document.getElementById("myButton");
      button.addEventListener('click', addTwoNumbers, false);
    </script>
  </body>
</html>

So, it seems you want to have the user input two numbers and do something with them.
As felgall rightly points out, the use of the prompt method is a little dated, but let’s stick with it anyway.

var nr1 = prompt("Please enter number","first number"),
    nr2 = prompt("Please enter a second number","second number");

Note you only need to use the var keyword once. You separate consecutive assignments with a comma.

Now, let’s calculate the result and give it a sensible name (not x).
Also, use the Number() method to convert string values to an integer.

var result = Number(nr1) + Number(nr2);

Okay, now we have that, let’s add a container for our results to the page, then get a reference to it:

<div id="result"></div>

var resultDiv = document.getElementById("result");

Now we can do what we want with the calculation.
Let’s make number one blue, number two red, the result green and display everything on a seperate line:

We can do this in a number of ways, for example:

var nr1Div = "<div class='blue'>First number: " + nr1 + "</div>",
    nr2Div = "<div class='red'>Second number: " + nr2 + "</div>",
    resDiv = "<div class='green'>Result: " + result + "</div>";

resultDiv.innerHTML = nr1Div + nr2Div + resDiv;

By using a block level element (divs), we can ensure that each is rendered on a new line.
You could also have added <br> tags manually.

resultDiv.innerHTML = "This is some text" + "<br />" + "with a line break";

Then we just have to add the CSS:

.blue{ color: blue;}
.red{ color: red;}
.green{ color: green;}

And that’s it :slight_smile:

Here’s the code:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Add two numbers</title>
    <style>
      .blue{ color: blue;}
      .red{ color: red;}
      .green{ color: green;}
    </style>
  </head>
  
  <body>
    <button id="myButton">Try it</button>
    <div id="result"></div>

    <script>
      function addTwoNumbers(){
        var nr1 = prompt("Please enter number","first number"),
            nr2 = prompt("Please enter a second number","second number"),
            result = Number(nr1) + Number(nr2),
            nr1Div = "<div class='blue'>First number: " + nr1 + "</div>",
            nr2Div = "<div class='red'>Second number: " + nr2 + "</div>",
            resDiv = "<div class='green'>Result: " + result + "</div>";

        resultDiv.innerHTML = nr1Div + nr2Div + resDiv;
      }

      var button = document.getElementById("myButton"),
          resultDiv = document.getElementById("result");
      button.addEventListener('click', addTwoNumbers, false);
    </script>
  </body>
</html>

Please, why is thi not working?
It is not showing proper color:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


<script>
function displayResult()
{
if (nr1>0){document.getElementById("nr1").style.color="green";}
else

{document.getElementById("nr1").style.color="red";}


}
</script>




</head>
<body>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="myFunction()">Try it</button>
<p>Nr #1: <b><span id="nr1"></span></b></p>
<p>Nr #2: <b><span id="nr2"></span></b></p>

<p>Se&#353;tevek: <span  id="sestevek"></span></p>
<p>Razlika: <span  id="razlika"></span></p>


<script>

function addiotion(nr1, nr2) {

	var sum = nr1 + nr2;
	return sum;
}

function differentiation(nr1, nr2) {

	var diff = nr1 - nr2;
	return diff;
}

function myFunction()
{
	var nr1=prompt("Please enter nr1","number1");
	var nr2=prompt("Please enter nr2","number2");

	nr1 = parseInt(nr1);
	nr2 = parseInt(nr2);

  
	document.getElementById("nr1").innerHTML= nr1;
	document.getElementById("nr2").innerHTML= nr2;
	
	document.getElementById("sestevek").innerHTML = addition(nr1, nr2);
	document.getElementById("razlika").innerHTML  = differentiation(nr1, nr2);
  
}
</script>
</body>
</html>

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Add two numbers</title>
    <style>
      .blue{ color: blue;font-weight:bold}
      .red{ color: red;}
      .green{ color: green;}
    </style>
  </head>

  <body>
    <button id="myButton">Try it</button>
    <div id="result"></div>

    <script>
      function addTwoNumbers(){
        var nr1 = prompt("Please enter number","first number"),
            nr2 = prompt("Please enter a second number","second number"),
            result = Number(nr1) + Number(nr2),
            nr1Div = "<if nr1<0 {div class='blue'> " + nr1 + "</div>",}else
{ "<div class='red'>" + nr2 + "</div>",}
            nr2Div = "<div class='red'>" + nr2 + "</div>",
            resDiv = "<div class='green'>Result: " + result + "</div>";

        resultDiv.innerHTML = nr1Div + nr2Div + resDiv;
      }

      var button = document.getElementById("myButton"),
          resultDiv = document.getElementById("result");
      button.addEventListener('click', addTwoNumbers, false);
    </script>
  </body>
</html>

Pleasem what ids wrong here?
Any pages with exampls maybe?
many thanks

This line is wrong. Try to work out why:

nr1Div = "<if nr1<0 {div class='blue'> " + nr1 + "</div>",}else
{ "<div class='red'>" + nr2 + "</div>",}

start off like so:

var nr1Div;
if(...){
  nr1Div = whatever;
} else {
  nr1Div = whatever;
}
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Try it</button>
<p id="nr1">
<p id="nr2">
<script>
function myFunction()
{
var nr1=prompt("Please enter nr1: ", "number1");
var nr2=prompt("Please enter nr2: ", "number2");




document.getElementById("nr1").innerHTML= nr1;
document.getElementById("nr2").innerHTML= nr2;
}

</script>
</body>
</html> 

Hello!
Here are 2 cosde parts:


<script type="text/javascript">

function sestevek(nr1, nr2) {
if (isNan(nr1)!==false){document.write("<font color='red'><b>This is not a number!</b></font>");}
var sum = nr1 + nr2;
return sum;
}

<script type="text/javascript">

function sestevek(nr1, nr2) {
if (nr1==0){document.write("<font color='red'><b>This is zero!!</b></font>");}
var sum = nr1 + nr2;
return sum;
}

It writes fine the second one: this is zero!
But, in first example, I do not get nice red bold letters telling "This is not a number!

Please, does soeone see where is the problem?
Many thanks!

Hello!
Please, I tried this online at w3schol, and I do nto get these 2 numbers each in its line, bu ttogether.

The same happens when I use string.
What is wrong?
Many thanks!

<!DOCTYPE html>
<html>
<body>

<p>Click the button to loop through a block of code five times.</p>
<button onclick="myFunction()">Try it</button>


<script>
function myFunction()
{
var a=7;
var b=6;
var c=a+b;
var d="Lep dan &#382;elim!!!"
document.writeln(a);
document.writeln(c);
}
</script>

</body>
</html>

P.s. Please, cansomeone tell me if it is ok to keep in this theme?
I am klicking “reply to thsi thread”.
Is that ok?
All the best!

Those commands have been dead for over 10 years now - as a beginner you shouldn’t even be seeing them referenced anywhere in the course or book you are learning from - unless you are learning history instead of programming in which case why are you worried about whether the code works or not in modern browsers as that code is for Netscape 4 and earlier.