Textbox value

How I mention this output in textbox defaultly without clicking on any button :

ORIGINALLY SCRIPT:

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {
    var x = "User-agent header sent: " + navigator.userAgent;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

I am doing

<!DOCTYPE html>
<html>
<body>
<input type="text" id="demo" />

<script>
    var x = "User-agent header sent: " + navigator.userAgent;
    document.getElementById("demo").innerHTML = x;

</script>

</body>
</html>

But it is not working? Help

Hi, maybe that should work? I haven’t tested it.

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {
    var x = "User-agent header sent: " + navigator.userAgent;
    document.getElementById("demo").innerHTML = x;
}
document.getElementById("myButton").click();
</script>

</body>
</html>

Yes it is working but it is under <p> tag , How I convert <p> tag to input type =" text" ?

<!DOCTYPE html>
<html>
<body>

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

<input type="text" id="demo"/>

<script>
function myFunction() {
    var x = "User-agent header sent: " + navigator.userAgent;
    document.getElementById("demo").value = x;
}
document.getElementById("myButton").click();
</script>

</body>
</html>

However why do you need a button at all?

No don’t need but How I remove that? When I am removing btn the textbox value delete.

I has done it
The script would be:

<!DOCTYPE html>
<html>
<body>

<input type="text" id="demo"/>

<script>
    var x = "User-agent header sent: " + navigator.userAgent;
    document.getElementById("demo").value = x;
document.getElementById("demo").click();
</script>

</body>

Just remove the last line with the ‘click()’.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.