Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a function to change currency symbol

Status
Not open for further replies.

MeantimeSteve

Programmer
Oct 13, 2004
2
GB
I am trying to run a function that will change a currency symbol on a page depending on a variable value. I am failry new to javascript as my main language is PHP so be kind when you take apart my code!

Here is what i have tried to date:

<script>
function showSymbol(currentCurrencyUsed) {
if(currentCurrencyUsed == "POUND") {
Symbol = "£";
document.test.currency.value = "POUND";
}
else {
Symbol = "&#8364;";
document.test.currency.value = "EURO";
}
return Symbol;
}
</script>

<body>
<form name="test">


<br>
<input type="text" name="currency" value="POUND"><br>
<script>
showSymbol(document.test.currency.value);
document.write(Symbol);
</script> <br>
<input type="button" value="Click Me" onClick="showSymbol(document.test.currency.value);">
</form>

</body>
</html>

Please can someone show me where i have gone wrong

Thanks in advance.
 
Things you could do:
(1) add a DIV tag where the currency will be and, instead of running the SCRIPT inside the BODY tag, call the function with the BODY tag's ONLOAD event:

Code:
<body [b]onload='showSymbol();'[/b]>
<form name="test">
  <br>
  <input type="text" name="currency" value="POUND"><br>
  [b]<div id="symbolDiv" style="float:left"></div>[/b]<br>
  <input type="button" value="Click Me" onClick="showSymbol(document.test.currency.value);">
 </form>
</body>

Then, simplify the showSymbol() code to:

Code:
function showSymbol() 
{
    var currentCurrencyUsed = document.test.currency.value;

    if(currentCurrencyUsed == "POUND")
        symbolDiv.innerHTML = "£";
    else
        symbolDiv.innerHTML = "&#8364;";
}

'still not the most user-friendly form (for example, user's HAVE to use ALL-CAPS or else they won't get the results they want), but I'm guessing this is a real scaled-down version of what you're really doing.

'hope that helps!

--Dave
 
Hi Dave.

Thanks for this. You are right, it is a scaled down version, with the currency box a hidden field and a variable set using php to write it out at the top of the page that defaults the currency to pound. Then, when the change currency button is clicked the hidden field is updated automatically to EURO so the value will always be correct - i just had it displayed so i could see it.

Thanks for the code BTW which worked perfectly!

Steve
 
Great!

By the way, as someone who likes to test code before offering solutions to questions posted here, I greatly appreciate your providing the mini-code sample you did!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top