daisy
daisy

Reputation: 357

Not able to call function on button click in Javascript html

I have below code i need to call function on click of button. but it is not atall calling those function. What is wrong in below code.

<!DOCTYPE html>
<html>
<body>

<input type="button" value="Copy Parent Text" id="CopyParent"  onclick="getRate("SEK", "USD")">
<input type="button" value="Copy Child Text" id="CopyChild"  onclick="getRate("USD", "SEK")">


<script type="text/javascript">

  function getRate(from, to) {
  alert("1");
    var script = document.createElement('script');
    script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D"+from+to+"%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
    document.body.appendChild(script);
  }

  function parseExchangeRate(data) {
    var name = data.query.results.row.name;
    var rate = parseFloat(data.query.results.row.rate, 10);
    alert("Exchange rate " + name + " is " + rate);
  }

</script>

</body>
</html>

Please help me on this.

Upvotes: 0

Views: 70

Answers (4)

abhi nagi
abhi nagi

Reputation: 19

Check the documentation on using onclick function. U have to specify the single quotes for parameters if u are using onclick

<input type="button" value="Copy Parent Text" id="CopyParent"  onclick="getRate('SEK', 'USD')">
<input type="button" value="Copy Child Text" id="CopyChild"  onclick="getRate('USD', 'SEK')">

Upvotes: 0

Nishant Saini
Nishant Saini

Reputation: 431

simple mistake "getRate('SEK', 'USD')" and "getRate('USD', 'SEK')"

<!DOCTYPE html>
<html>
<body>

<input type="button" value="Copy Parent Text" id="CopyParent"  onclick="getRate('SEK', 'USD')">
<input type="button" value="Copy Child Text" id="CopyChild"  onclick="getRate('USD', 'SEK')">


<script type="text/javascript">

  function getRate(from, to) {
  alert("1");
    var script = document.createElement('script');
    script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D"+from+to+"%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
    document.body.appendChild(script);
  }

  function parseExchangeRate(data) {
    var name = data.query.results.row.name;
    var rate = parseFloat(data.query.results.row.rate, 10);
    alert("Exchange rate " + name + " is " + rate);
  }

</script>

</body>
</html>

Upvotes: 0

ADyson
ADyson

Reputation: 61904

You have a syntax error because you're enclosing double quotes within double quotes. If you look in the browser's console when you click the button it will show you the error.

You can change the HTML like this to resolve it:

<input type="button" value="Copy Parent Text" id="CopyParent"  onclick="getRate('SEK', 'USD')">
<input type="button" value="Copy Child Text" id="CopyChild"  onclick="getRate('USD', 'SEK')">

Upvotes: 1

Super User
Super User

Reputation: 9642

You have add double quot inside double quote "getRate("USD", "SEK")". Just change you html like below:

<input type="button" value="Copy Parent Text" id="CopyParent"  onclick='getRate("SEK", "USD")'>
<input type="button" value="Copy Child Text" id="CopyChild"  onclick='getRate("USD", "SEK")'>

Upvotes: 0

Related Questions