123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1 class="title"> Random User Generator</h1>
<div class="user-profile">
	<img id="avatar" src="https://pbs.twimg.com/profile_images/743138182607175680/ZJzktgBk_400x400.jpg" />
    <div id="fullname">Jon Snow</div>
  <div id="username">
  	kingofnorth
  </div>
    <div class="description">
      <div>Email: <span id="email">jon@hotmail.com</span></div>
      <div>City: <span id="city">Winterfell</span></div>
  </div>
  <div class="footer">
    <button id="btn">Next User!</button>
 </div>
</div>
            
          
!
            
              /* CSS design originally by @jofpin, tweaked by Colt Steele */
@import url(https://fonts.googleapis.com/css?family=Raleway|Varela+Round|Coda);

body {
  background: #ecf0f1;
  padding: 2.23em;
}

.title {
  color: #2c3e50;
  font-family: "Coda", sans-serif;
  text-align: center;
}
.user-profile {
  margin: auto;
	width: 27em; 
  height: 11em;
  background: #fff;
  border-radius: .3em;
}

.user-profile  #fullname {
  margin: auto;
  margin-top: -4.40em;
  margin-left: 5.80em;
  color: #16a085;
  font-size: 1.53em;
  font-family: "Coda", sans-serif;
  font-weight: bold;
}

#username {
  margin: auto;
  display: inline-block;
  margin-left: 10.43em;
  color: #3498db;
  font-size: .87em;
  font-family: "varela round", sans-serif;
}

.user-profile > .description {
  margin: auto;
  margin-top: 1.35em;
  margin-right: 3em;
  width: 18em;
  color: #7f8c8d;
  font-size: .87em;
  font-family: "varela round", sans-serif;
}

.user-profile > img#avatar {
	padding: .7em;
  margin-left: .3em;
  margin-top: .3em;
  height: 6.23em;
  width: 6.23em;
  border-radius: 18em;
}


.footer {
	margin: 2em auto;
	height: 3.70em;
  background: #16a085;
  text-align: center;
  border-radius: 0 0 .3em .3em;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: background 0.1s;
}

button {
  color: white;
  font-family: "Coda", sans-serif;
  text-align: center;
  font-size: 20px;
  background: none;
  outline: none;
  border: 0;
}

.footer:hover {
  background: #1abc9c;
}
            
          
!
            
              // If you're coming from the LoF, I know this code isn't DRY at all
// My objective was simply to understand FetchAPI and worry later about DRY code

var btn = document.querySelector("#btn");
var avatar = document.querySelector("#avatar");
var fullName = document.querySelector("#fullname");
var userName = document.querySelector("#username");
var email = document.querySelector("#email");
var city = document.querySelector("#city");
var url = "https://randomuser.me/api/";

btn.addEventListener("click", function () {
  fetch(url)
   .then(function(res) {
    return res.json();
  })
  .then(function(res) {
    console.log(res["results"]["0"]);
    fullName.innerText = res["results"]["0"].name.first.charAt(0).toUpperCase()     + res["results"]["0"].name.first.slice(1) + " " + res["results"]["0"].name.last.charAt(0).toUpperCase() + res["results"]["0"].name.last.slice(1);
    userName.innerText = res["results"]["0"].login.username;
    email.innerText = res["results"]["0"].email;
    city.innerText = res["results"]["0"].location.city;
    avatar.src = res["results"]["0"].picture.medium;
  })
})


// fetch(url)
// .then(handleErrors)
// .then(parseJSON)
// .then(updateProfile)
// .catch(printError)

// btn.addEventListener("click", function() {
//   var url = "https://randomuser.me/api/";
//   fetch(url).then(function(response) {
//     console.log("The data is: " + response);
//   }).then(function(data) {
//     console.log(data.results);
//   })
// });

// fetch(url).then(function(response){
//   console.log(response);
//   return response.json()
// })
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console