0

I'm trying to change text in <p> tags using Javascript. Here's what I've done so far, but it's not working. Any tips?

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta charset="UTF-8"/>
  </head>

  <script type="text/javascript">
     var mText = prompt("sheiyvanet satauri");
     function myFunction() {
       document.getElementById("demo").innerHTML = mText;
     }
  </script>
  <body>
    <p id="demo"> </p>
  </body>
</html>
Daniel Olszewski
  • 12,152
  • 4
  • 52
  • 58
Jhon Dhoe
  • 43
  • 6
  • Possible duplicate of [Using a prompt to change text inside of a div](http://stackoverflow.com/questions/17180378/using-a-prompt-to-change-text-inside-of-a-div) – Aᴍɪʀ Dec 15 '15 at 07:56

3 Answers3

0

If you want to control when the event happens, add it to an onload or click function:

window.onload = myFunction();


function myFunction() {
  var mText = prompt("sheiyvanet satauri");
  document.getElementById("demo").innerHTML = mText;
}
<p id="demo"></p>
sideroxylon
  • 3,977
  • 1
  • 18
  • 33
0

You have to call the function that you just wrote.

And, you need to execute the function after whole page loads, otherwise the element that you want to change the text inside is not created yet.

You can move the script to the end of the page.

<html>
    <head>
        <title></title>
        <meta charset="UTF-8"/>
    </head>

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

    <script type="text/javascript">
        var mText = prompt("sheiyvanet satauri");
        function myFunction() {
            document.getElementById("demo").innerHTML = mText;
        }

        myFunction();

    </script>
</html>

Working Plunker.

Aᴍɪʀ
  • 6,664
  • 3
  • 36
  • 48
0
<!DOCTYPE html>
 <html>
<head>
  <title></title>
  <meta charset="UTF-8"/>
</head>


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


<script type="text/javascript">
var mText = prompt("sheiyvanet satauri");
  function myFunction() {
    document.getElementById("demo").innerHTML = mText;
  }
 myFunction();

Need to call function myFunction()

Shailesh Katarmal
  • 2,587
  • 1
  • 10
  • 15