cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

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.

            
              <h1>New York Times Bestsellers</h1>
	<h2 id="list-title"></h2>
	<ol id="data"></ol>

	<h2>Choose a list to look at:</h2>
	<form id="form">
		<div>
		    <input type="radio" id="paperback-fiction"
		           name="list" value="paperback-fiction" checked>
		    <label for="paperback-fiction">Paperback Fiction</label>
		</div>
		<div>
		    <input type="radio" id="hardback-fiction"
		           name="list" value="hardback-fiction">
		    <label for="hardback-fiction">Hardback Fiction</label>
		</div>
		<div>
		    <input type="radio" id="paperback-nonfiction"
		           name="list" value="paperback-nonfiction">
		    <label for="paperback-nonfiction">Paperback Non-fiction</label>
		</div>
		<div>
		    <input type="radio" id="science"
		           name="list" value="science">
		    <label for="science">Science</label>
		</div>
		<div>
    		<button type="submit" id="submit">Submit</button>
    	</div>
	</form>
	<p id="status"></p>
            
          
!
            
              body {
			font-family: sans-serif;
			font-size: 1em;
			max-width: 700px;
		}

		h1, h2, li, form {
			margin: 50px;
		}
		form div {
			display: block;
		}

		li {
			counter-increment: li;
			min-height: 200px;
			box-shadow: 0 4px 6px 0 hsla(0, 0%, 0%, 0.2);
		}

		li::before {
			content: counter(li);
			font-size: 3em;
			display: inline;
		}

		ol {
			list-style: none;
			counter-reset: li;
		}

		button {
			font-size: 1em;
			margin-top: 1em;
		}

		img {
			display: inline-block;
			float: left;
			width: 100px;
			height: auto;
			padding: 10px;
		}

		.up {
		    width: 0px;
		    height: 0px;
		    border-left: 12px solid transparent;
		    border-right: 12px solid transparent;
		    border-bottom: 12px solid #000;
		    float: right;
		    margin-right: 10px;
		}

		.down {
			width: 0px;
		    height: 0px;
		    border-left: 12px solid transparent;
		    border-right: 12px solid transparent;
		    border-top: 12px solid #000;
		    float: right;
		    margin-right: 10px;
		}

		.same {
			width: 0px;
		    height: 0px;
		    border-top: 12px solid transparent;
		    border-bottom: 12px solid transparent;
		    border-right: 12px solid #000;
		    float: right;
		    margin-right: 10px;
		}
            
          
!
            
              

	// Get page elements
	let ol = document.getElementById("data");
	let status = document.getElementById("status");
	let form = document.getElementById("form");
	let listTitle = document.getElementById("list-title");

	// Set up API connection
	const urlBase = "http://api.nytimes.com/svc/books/v3/lists/";
	const apiKey = "INSERT YOUR API KEY HERE";

	let bestsellers = (url) => {
		fetch(url).then((response) => response.json())
		.then(( response ) => {
			listTitle.textContent = response.results.list_name + ", " + response.results.bestsellers_date;
			response.results.books.map(book => {
				let li = document.createElement("li");

				// Work out up/down indicator
				let p = document.createElement("p");
				if ((book.rank_last_week === 0) || (book.rank < book.rank_last_week)) {
					p.classList.add("up");
				} else if (book.rank_last_week == book.rank) {
					p.classList.add("same");
				} else {
					p.classList.add("down");
				}
				li.appendChild(p);

				// Get image, title, and description
				let img = document.createElement("img");
				img.src = book.book_image;
				li.appendChild(img);
				p = document.createElement("p");
				p.textContent = book.title + " by " + book.author;
				li.appendChild(p);
				p = document.createElement("p");
				p.textContent = book.description;
				li.appendChild(p);

				// Work out if new entry
				p = document.createElement("p");
				let lastWeek = (book.rank_last_week == 0) ? "New entry" : book.rank_last_week;
				p.textContent = "This week: " + book.rank + "  Last week: " + lastWeek;
				li.appendChild(p);
				
				ol.appendChild(li);
				});
			status.textContent="";
			})

		.catch(function(error) {
	    console.log(JSON.stringify(error));
	  	}); 
	}

	let clicked = (e) => {
	 	e.preventDefault();
	 	ol.textContent = "";
	 	let urlString = "";

	 	// Return different list from API depending on which option selected by user
	 	if (document.getElementById('paperback-fiction').checked) {
	 		urlString = "trade-fiction-paperback.json"
		} else if (document.getElementById('paperback-nonfiction').checked) {
			urlString = "paperback-nonfiction.json";
		} else if (document.getElementById('hardback-fiction').checked) {
			urlString = "hardcover-fiction.json";
		} else if (document.getElementById('science').checked) {
			urlString = "science.json";
		}
	 	status.textContent = "Loading...";
		let url = urlBase + urlString + apiKey;
		return bestsellers(url);
	}

	form.addEventListener("submit", clicked);

            
          
!
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.
Loading ..................

Console