1

Here's a bit of code from a simple currency converter I'm building. The calculate() script runs fine, but the clear() code refuses to work and it appears the same??

<a href="#" OnCLick="calculate();">US - CDN</a>
<a href="#" OnCLick="clear();">Clear</a>

<script type="text/javascript">
    function clear() {
            document.getElementById('us-cdn1').innerHTML = '';
    }
</script>
<script type="text/javascript">
    function calculate() {
        var amount = parseFloat(document.getElementById("amount").value);
        var result = document.getElementById("amount");
            result.value = (amount).toFixed(2);
        var result = document.getElementById("amount1");
            result.value = (amount * .9861932938856016).toFixed(2); 
            document.getElementById('us-cdn').innerHTML = 'US';
            document.getElementById('us-cdn1').innerHTML = 'CDN';
    }

</script>
publisher
  • 11
  • 4
  • sorry, bad post... the tag was closed... The calculate script works fine... just the clear is failing – publisher Feb 03 '13 at 18:03
  • Never mind, I see your comment – Austin Mullins Feb 03 '13 at 18:04
  • btw... appreciate your quick answer... anything else look wrong there... I'm really scratching my head! – publisher Feb 03 '13 at 18:04
  • also btw... if I execute from withing the ref it works fine there... I just want the script outside so I can call it from if statements I'll be building in later... but if this simple thing fails... I don't like my odds of adding that code! :) – publisher Feb 03 '13 at 18:06
  • 1
    I recommend you use jQuery and add your listeners afterwards. Have a look at [this fiddle](http://jsfiddle.net/9bew2/) – MCL Feb 03 '13 at 18:13
  • Why are there two script tags? What happens if you include the clear() function in the second script tag? – Austin Mullins Feb 03 '13 at 18:15
  • just make the C capital of clear function – Arpit Feb 03 '13 at 18:19
  • @Arpit that's defying the holy javascript naming convention rules – bicycle Feb 03 '13 at 18:21
  • 1
    Here is a good explanation about clear() http://stackoverflow.com/questions/7165570/is-clear-a-reserved-word-in-javascript – Arpit Feb 03 '13 at 18:22
  • Thanks everyone iPhoneing this in remotely but The public function is it clearField() Works a charm I'll look further at the more elegant suggestions but I really appreciate the responses everyone!! – publisher Feb 03 '13 at 19:17

1 Answers1

1

clear() is a public function. Change it into:

<a href="#" onclick="clearField();">Clear</a>

<script type="text/javascript">
function clearField() {
        document.getElementById('us-cdn1').innerHTML = '';
}
</script>
bicycle
  • 8,315
  • 9
  • 52
  • 72
  • +1 - Only as a note to be 100% accurate it's actually `onclick`, you can read about it here: [onclick or onClick?](http://stackoverflow.com/q/4380719/1456376) – insertusernamehere Feb 03 '13 at 18:23