0

I'm building a basic website. One of the requirements is that the user enters a number, which is processed by JavaScript and then the processed data is output. Here is what I've come up with:

<HTML>
<HEAD>
<TITLE>Test Input</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function testResults (form) {
    var TestVar = form.inputbox.value+1;
    // Here, I want to return TestVar as a h1 tag in HTML
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR>
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)">
</FORM>
</BODY>
</HTML>

So that's what I want to do. I'm sorry if this is a stupid question; I'm new to web dev. Thanks

1
  • Don't forget to convert the input value to a number if you want to do numeric addition instead of string concatenation. Commented Sep 16, 2020 at 18:31

2 Answers 2

1

The easy/ugly way to this is :

Add H1 tag

<h1 id="title"></h1>
<FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR>
...

in your function :

function testResults (form) {
    var TestVar = form.inputbox.value+1;
    const title = document.querySelector('#title')
    title.innerText = TestVar
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can try document.write('Text'); if that suits you, or you can make an invisible html element like div or something and then write to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.