Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Javascript Help

<body> <div class="container" style="text-align: center; font-family: Raleway; height: 220px:"> <h1 style="color: white; background-color: red"><i class="fab fa-bitcoin" style="color: white; padding: 45px;" href="bitcoin.com"></i> Bitcoin - decentralized digital currency. </h1> <div id="data"></div> <hr> <hr> </div>

</body> </html>

<script type="text/javascript"> var xmlhttp = new XMLHttpRequest(); var url = "https://api.coindesk.com/v1/bpi/currentprice.json"; xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var json = JSON.parse(this.responseText); parseJson(json); }
}; xmlhttp.open("GET", url, true); xmlhttp.send(); function parseJson(json) { var euroValue = "€" + json["bpi"]["EUR"]["rate"]; document.getElementById("data").innerHTML = "<br />" + euroValue; } </script>

So this gets the BTC price, how would I make it, so once you click a button, it refreshes it.

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

You just need to add a button to your HTML, wrap your javascript in a function and call the function on the button click event.

Does this PEN show your desired result. Notice the commented out function call at the end, uncomment that line so it started with a value, if desired. Also, the button styling/CSS is just copied stuff for fun.