user4708518
user4708518

Reputation:

Twitch API -- Get Name

just wondering if anyone who's experienced using JQuery or twitch API could help with this. Basically I am trying to get a username, but I do not want to have to click a button or display it in an input box.

Here is the code from the API examples:

  $('#get-name button').click(function() {
    Twitch.api({method: 'user'}, function(error, user) {
      $('#get-name input').val(user.display_name);
    });
  })

So does anyone know how I would get the username without displaying it in an input box or have to click a button to display their username.

Upvotes: 0

Views: 572

Answers (1)

Matt Wetmore
Matt Wetmore

Reputation: 300

You just need to make a call to Twitch.api as in the code example, but you don't need to worry about doing it inside of a click event handler.

Twitch.api({method: 'user'}, function(error, user) {
  // this code runs after the Twitch API returns you the user data.
  // whatever you want to do with the username goes in here.
  // for example:
  console.log(user.display_name);
});

Let's say that somewhere in the HTML for your page, you have the following bit for displaying the username like you say:

<div class="greeting">
    Welcome, <span class="username"></span>
</div>

Then you can use

Twitch.api({method: 'user'}, function(error, user) {
  $('.greeting .username').text(user.display_name);
});

to fill the <span> with the user's name.

Upvotes: 1

Related Questions