0

I have tried multiple ways to get result but i am unable to do so .. I am quite new in Javascript ..

For an example:

User input value "5" in "results2". Upon clicking the SGD button, I want the value in input to be change to the new value which will be UserInput * SGDRate .

This is my HTML code

<p class="heading">
This is a currencry converter for Singapore To Malaysia Currency </P>

<form name="currency">
<input type="text" name="results2">
<input type="button" value=SGD name="SGD" onclick="displayConversion(SGD.value)">
<input type="button" value="Please Click to find what is the latest Rate" name="help" onclick = "rates()">
</form>

As for my Javascript file

I know that I need to create another function but I am unsure where do I start.

var SGDrate = 2.92;
var convertrate = inputUser * SGDrate;

function displayConversion(n1)
{
currency.results2.value=n1;
}

2 Answers 2

0

You should pass the value of input.result2 when using onclick event like this :

<input type="button" value=SGD name="SGD" onclick="displayConversion(document.getElementsByName('results2')[0].value)">

Then in your script you should calculate your equation into the function :

var SGDrate = 2.92;

function displayConversion(n1)
{
  var convertrate =n1 * SGDrate;
  currency.results2.value=convertrate;
}

See your example after those edits:

var SGDrate = 2.92;

function displayConversion(n1)
{
  var convertrate =n1 * SGDrate;
  currency.results2.value=convertrate;
}
<p class="heading">
  This is a currencry converter for Singapore To Malaysia Currency </p>

<form name="currency">
  <input type="text" name="results2">
  <input type="button" value=SGD name="SGD" onclick="displayConversion(document.getElementsByName('results2')[0].value)">
  <input type="button" value="Please Click to find what is the latest Rate" name="help" onclick = "rates()">
</form>

You also should check the input value by the user if it is float or not, in the following snippet I checked the value entered by the user and alert message if it is not float

var SGDrate = 2.92;

function displayConversion(n1)
{
  if( !isNaN(parseFloat(n1)) ){
    var convertrate = parseFloat(n1) * SGDrate;
    currency.results2.value=convertrate;
  }
  else{
    alert("you must enter a number")
  }
}
<p class="heading">
  This is a currencry converter for Singapore To Malaysia Currency </p>

<form name="currency">
  <input type="text" name="results2">
  <input type="button" value=SGD name="SGD" onclick="displayConversion(document.getElementsByName('results2')[0].value)">
  <input type="button" value="Please Click to find what is the latest Rate" name="help" onclick = "rates()">
</form>

Sign up to request clarification or add additional context in comments.

Comments

0

You made a few mistakes in your code:

  1. You forgot to indent the code.
  2. You closed the paragraph with </P> instead of </p>.
  3. You need to pass results2.value to displayConversion, not SGD.value.
  4. convertrate needs to be a function in order to be evaluated more than once.

Also you could have added an id attribute to results2 to make it easier to change its value.

Here is the code with those problems fixed.

function convertrate(rate) {
  var SGDrate = 2.92;
  return rate * SGDrate;
}

function displayConversion(rate) {
  var results2 = document.querySelector('#results2');
  results2.value = convertrate(rate);
}
<p class="heading">
  This is a currencry converter for Singapore To Malaysia Currency
</p>

<form name="currency">
  <input type="text" id="results2" name="results2">
  <input type="button" value=SGD name="SGD" onclick="displayConversion(results2.value)">
  <input type="button" value="Please Click to find what is the latest Rate" name="help" onclick = "rates()">
</form>

Comments

Your Answer

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