ENGINEERING NOTES

jQuery Button Call API

Search articles & projects

This is a nice and quick note of how to create a button on a web page that will be used to run something.

In this example I am going to be shuffling a deck of cards from the Deck of Cards API.
https://www.deckofcardsapi.com

Very basic, a button on a web page. When clicked the API to shuffle a deck is called. Nothing is returned to the web page. Checking the console we will see it. This can be expanded on later.

This will be done on jsfiddle.net as it allows us to see everything easily for this type of quick testing.
https://jsfiddle.net/1x8fbu5a/

On to the details…

HTML

<a href="#" id="test">
  <button>
  Test
  </button>
</a>
<div id="dumpingGround"></div>

jQuery

$(function() {
    $('a#test').on('click', function(e){

        e.preventDefault()
          $.getJSON("https://www.deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1",
            function(data) {
                console.log(data)
                $('#dumpingGround').html(data)
        });
        return false;
    });
});

KEEP EXPLORING

Engineering is a work in progress.

Follow the code, explore the projects, or get in touch.

Connect on LinkedIn