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 Calculation

Featured Replies

Hi guys, I'm not the best at JavaScript, can somebody be so kind and review the following and see if there is anything they notice that could be causing this form not to work properly.

 

Issue: After submit, form displays correct calculations for a split second and then resets to be blank.

 

Script

function CalculateSum(xtext, ztext, form)
{
var x = parseFloat(xtext);
var z = parseFloat(ztext);
form.Yield.value = (x / z);
}

function ClearForm(form)
{
form.input_x.value = "";
form.input_z.value = "";
form.Yield.value = "";
}

function doClear(theText) 
{
    if (theText.value == theText.defaultValue)
{
        theText.value = "";
   }
}

Form

<form name="Calculator" method="post" runat="server" onSubmit="return checkform()">
£ <input name="input_x" type="text" />
£ <input name="input_z" type="text" />
<input name="Yield" readonly="readonly" type="text"  /> %

<input name="ClearButton2" type="button" onclick="ClearForm(this.form)" value="clear" />

<input type="submit" name="AddButton" onClick="CalculateSum(this.form.input_x.value, this.form.input_z.value, this.form)" value="submit" />
</form>

 

This should be so straight forward, I'm sure I'm missing something silly.

 

Thanks in advance!

Got a live example?

 

I've not done this for a while, but isn't a page refresh default behaviour? Which would require a return false; to counteract it.

  • Author

Got a live example?

 

I've not done this for a while, but isn't a page refresh default behaviour? Which would require a return false; to counteract it.

 

Homer Simpson moment i think it was here!

 

return false did the trick. I don't know why but an old version of the same calculator worked without it for some reason so it slipped my mind to include it in this one!

 

Thank you so much! +1

Sometimes it just takes a fresh set o' eyes ;)

 

Thanks for the +1, people always forget to do that to me :(

 

 

  • Author

Sometimes it just takes a fresh set o' eyes ;)

 

Thanks for the +1, people always forget to do that to me :(

 

 

Yes indeed! I'm a web developer on a creative team of 4 and the other guys know nothing of JavaScript at all so they didn't even bother taking a look, but, once again this place gave me my answer within an hour! :) I love it!

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.