In: Computer Science
This is a question on the inputs and outputs on Basic javascript application
Are: 1 in the first box, 2 in the second box, 3 in the third box, and 4 in the fourth box
Modify the HTML and javascript file in order to
ii) Accesses all the html elements, then adds the four input values as strings and sends the result
to the paragraph element and the span element
<!DOCTYPE html>
<html>
<head>
<title> Javascript Example </title>
<script src ="please3_test.js" type ="text/javascript" defer> </script>
</head>
<body>
<div>
<input id ="num1" type ="text" size ="3"/></br>
<input id ="num2" type ="text" size ="3"/></br>
<input id ="num3" type ="text" size ="3"/></br>
<input id ="num4" type ="text" size ="3"/></br>
<span id ="answer"> Show Output Here</span><br/>
<button id ="btn">Compute </button><br/>
</div>
</body>
</html>
please3_test.js
document.getElementById("btn").onclick = compute;
function compute() {
var input1 = document.getElementById("num1");
var input2 = document.getElementById("num2");
var input3 = document.getElementById("num3");
var input4 = document.getElementById("num4");
var answer2 = document.getElementById("answer");
var result = " ";
result= input2.value + input3.value + input4.value;
oddstr = 4;
var finstr = result + oddstr;
answer2.innerHTML = finstr;
result1 = parseInt(input1.value) +parseInt(input2.value) +parseInt(input3.value);
input4.value = result1;
}
If the user inputs 1,2,3,4 respectively, the output that will be displayed in the 4th textbox will be 6 and the output in the answer span tag will be 2344.
For the HTML and JS modification question, please see the code below. The comments are provided for the better understanding of the logic.
HTML File
<!DOCTYPE html>
<html>
<head>
<title> Javascript Example </title>
<script src ="please3_test.js" type ="text/javascript" defer> </script>
</head>
<body>
<div>
<input id ="num1" type ="text" size ="3"/></br>
<input id ="num2" type ="text" size ="3"/></br>
<input id ="num3" type ="text" size ="3"/></br>
<input id ="num4" type ="text" size ="3"/></br>
<span id ="answer"></span><br/>
<!--Declare the h1 tag and span tag here. Provide the id field with a suitable name-->
<h1 id="h1tag"></h1><br/>
<span id="spantag"></span><br/>
<button id ="btn">Compute </button><br/>
</div>
</body>
</html>
Javascript file
document.getElementById("btn").onclick = compute;
function compute() {
var input1 = document.getElementById("num1");
var input2 = document.getElementById("num2");
var input3 = document.getElementById("num3");
var input4 = document.getElementById("num4");
var result = " ";
//Add the below lines of code.
//The below line will concatenate the input from all the 4 fields as string.
result = input1.value + input2.value + input3.value + input4.value;
//The below lines will display the result in the newly created tags.
document.getElementById("h1tag").innerHTML = result;
document.getElementById("spantag").innerHTML = result;
}
The screenshots of the code and output are provided below.