Skip to content

Instantly share code, notes, and snippets.

View avand's full-sized avatar

Avand Amiri avand

View GitHub Profile
var form = document.querySelector("#new-item-form");
var list = document.querySelector("#todo-list");
form.addEventListener("submit", formSubmitted);
loadList();
function formSubmitted(event) {
event.preventDefault();
// Assume that elsewhere we've defined the createItem function...
// Write a value, "avand", to a given key, "username"...
localStorage.setItem("username", "avand");
// Read the value of a given key...
var username = localStorage.getItem("username");
navigator.geolocation.getCurrentPosition(displayCurrentPosition);
function displayCurrentPosition(position) {
var coords = position.coords;
console.log(coords.latitude, coords.longitude);
}
WHEN the page loads:
- Get the form
- Add an event listener
WHEN the form is submitted:
- Prevent the default form submission
- Get the value of the input
- Add the input's value to the end of the base URL ("https://api.github.com/users/")
- Make an HTTP request to the full URL
WHEN the response comes back:
- Parse the JSON string response (e.g., JSON.parse(response))
$("a").css({
"color": "blue",
"background-color": "white",
"border": "1px solid black"
})
// Get all the list items, update their text, and set a class...
$("li").text("new text").addClass("todo")
$.get("https://api.github.com/users/avand", showUserInfo);
function showUserInfo(response) {
// Handle the response...
}
// Get all the todos on the page...
var $listItems = $("li.todo");
// Add a class to them all...
$listItems.addClass("complete");
@avand
avand / get.js
Last active July 20, 2016 00:18
// Declare our get function...
function get(url, callback) {
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.addEventListener("readystatechange", handleReadyStateChange)
function handleReadyStateChange() {
if (request.readyState == 4 && request.status == 200) {
<li class="movie">
<div class="movie-title">
<a href="http://www.imdb.com/title/tt0106611" target="_blank">
Cool Runnings
</a>
</div>
<div class="movie-release-date">1993</div>
</li>