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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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

Console