Search code examples
javascripthtmlprompt

object HTMLTextAreaElement javascript response on document.write


Why can't I get var yourName to show in the text through Javascript/Jquery? With all the things I've tried, it gives me [object HTMLTextAreaElement] as a response. What am I doing wrong?

Javascript:

var yourName = window.prompt("please enter name") 

HTML + Javascript:

<div id="info"><p>
<script>document.write(yourName)</script>, your job is...
</p><div>

See here: https://jsfiddle.net/fbt4rjjt/


Solution

  • It is not PHP it won't render your html "in place". You have to create a "container" and then for example change its innerHTML.

    You can achieve it for example in this way:

    HTML:

        <div id="info"><p>
            <b id="thename"></b>, your job is...
        <div>
    

    JS:

        var yourName = window.prompt("please enter name") 
        document.getElementById("thename").innerHTML = yourName
    

    check out this here: https://jsfiddle.net/cqv8jfgj/