Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JavaScript form calculation

Featured Replies

I store the users data in a variable via a form. I then perform calculation and print back to the screen. How do I get this to print back without page reload?

<input type="text" id="inpt">
<input type="submit" value="Calculate" id="submit" onclick="calculate()">
<script>
function calculate()
{
	var input = document.getElementById("inpt").value;
	var rate = 10;
	var result = input * rate
	document.write(result);
}
</script>

First of all: don't use inline JavaScript such as your click handler, that should live within your calculate function.

 

Secondly once you're done put the script in a external file.

 

Finally NEVER use document.write it's gonna write over the whole page. you need to select a DOM element and put the result on that element.

 

Here's a refactored version for you.

<input type="text" id="inpt">
<input type="submit" value="Calculate" id="submit">
<div id="result"></div>
(function(){
    function calculate() {
        var input = document.getElementById("inpt").value,
            resultEl = document.getElementById("result"),
            rate = 10,
            result = input * rate;
        // check if the value is not a number
        if (isNaN(result)) {
            resultEl.innerHTML = "Please enter a number";
            // exit the function
            return false;
        }
        // put the result inside the div.        
        resultEl.innerHTML = result;
    }
    // store btn in a variable
    var btn = document.getElementById('submit');
    // add click handler to btn that calls the calculate function.
    btn.addEventListener('click', calculate);
}());

Fiddle http://jsfiddle.net/LpLa4goa/

Edited by rbrtsmith

Either that^ or alternatively create the result element with js: The createElement() method creates a new dom element and you can attach the string data to it with appendChild()

https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

Either that^ or alternatively create the result element with js: The createElement() method creates a new dom element and you can attach the string data to it with appendChild()

https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

 

This could be a better approach actually if you don't want that element polluting your HTML :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.