0

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.

4 Answers 4

1

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')">
Sign up to request clarification or add additional context in comments.

1 Comment

These guys are called rep diggers. Sitting on SO whole day and just camping for trivial questions, like how to declare variable in JS. Sad, but true.
0

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>

2 Comments

how about pointing out what and where the error is, instead of just a big code dump, which includes parts of the code not even relevant to the error? OP would have to compare everything to see what's changed. It would take 10 seconds to add an explanation.
@Nishant Saini Thanks. I choose yuor answer because you were first one to answer this post.
0

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")'>

Comments

0

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')">

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.