I have a Javascript function which does the following:
However instead of displaying the result in an alertbox I would like to display the result in an DIV element on the webpage under the form. Any ideas how I can accomplish this?
My code looks as follows:
<script type="text/javascript">
function calc()
{
var total = 0;
var course = 0;
var nrOfLessons = 0;
var vat = 0;
course = Number(document.getElementById("course").value)
nrOfLessons = Number(document.getElementById("nrOfLessons").value)
total =(course * nrOfLessons)
vat = total * 0.15
total = total+ vat;
window.alert(total)
}
</script>
<form id="booking">
<strong>COURSE: </strong>
<select id="course">
<optgroup label="English Courses">
<option value="500">Beginner English</option>
<option value="700">Mid-Level English</option>
<option value="1000">Business English</option>
</optgroup>
<optgroup label="Thai Courses">
<option value="500">Introduction to Thai</option>
<option value="700">Pasa Thai</option>
<option value="1000">Passa Thai Mak</option>
</optgroup>
</select>
<select id="nrOfLessons">
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
Thanx in advance for all the help
A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.
Using JavaScript In vanilla JavaScript, you can use the native createElement() method to create an HTML <div> element and the appendChild() method to append the <div> element to another container.
To display the result of a function as HTML, you can use −. document.getElementById ().innerHTML.
Using innerHTML. To access an HTML element, JavaScript can use the document.getElementById(id) method. The id attribute defines the HTML element. The innerHTML property defines the HTML content:
JavaScript can "display" data in different ways: 1 Writing into an HTML element, using innerHTML. 2 Writing into the HTML output using document.write (). 3 Writing into an alert box, using window.alert (). 4 Writing into the browser console, using console.log ().
Writing into the browser console, using console.log (). To access an HTML element, JavaScript can use the document.getElementById (id) method. The id attribute defines the HTML element.
Use innerHTML
<script type="text/javascript">
function calc()
{
var total = 0;
var course = 0;
var nrOfLessons = 0;
var vat = 0;
course = Number(document.getElementById("course").value)
nrOfLessons = Number(document.getElementById("nrOfLessons").value)
total =(course * nrOfLessons)
vat = total * 0.15
total = total+ vat;
document.getElementById('total').innerHTML = total;
}
</script>
<div id="total"></div>
Give your <div>
an id, such as...
<div id="resultDiv"></div>
Then in your javascript set the .innerHTML
property...
document.getElementById("resultDiv").innerHTML = total.toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With