Last active
December 18, 2015 02:09
-
-
Save jpetitcolas/5709085 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Testing query selectors</title> | |
<ul id="fruits"> | |
<li>Banana</li> | |
<li>Peach</li> | |
<li>Lemon</li> | |
<li>Pineapple</li> | |
</ul> | |
<script> | |
$("#fruits li:nth-child(3)").css("color", "green"); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Testing query selectors</title> | |
<ul id="fruits"> | |
<li>Banana</li> | |
<li>Peach</li> | |
<li>Lemon</li> | |
<li>Pineapple</li> | |
</ul> | |
<script> | |
document.querySelector("#fruits li:nth-child(3)").style.color = "green"; | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myElement = $("#myElement"); | |
// Add selected class if not already set. Remove otherwise. | |
if(!myElement.hasClass("selected")) { | |
myElement.addClass("selected"); | |
} else { | |
myElement.removeClass("selected"); | |
} | |
// Equivalent to above code | |
myElement.toggleClass("selected"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myElement = document.getElementById("myElement"); | |
// Add selected class if not already set. Remove otherwise. | |
if(!myElement.classList.contains("selected")) { | |
myElement.classList.add("selected"); | |
} else { | |
myElement.classList.remove("selected"); | |
} | |
// Equivalent to above code | |
myElement.classList.toggle("selected"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="myDiv" class="fadable"></div> | |
<p> | |
<button id="fadeButton">Fade</button> | |
</p> | |
<script> | |
$("#fadeButton").click(function() { | |
$("#myDiv").fadeToggle(); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.fadable { | |
opacity: 1; | |
-webkit-transition: opacity 400ms ease; | |
-moz-transition: opacity 400ms ease; | |
-o-transition: opacity 400ms ease; | |
transition: opacity 400ms ease; | |
} | |
.fadable.fadeout { | |
opacity: 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="myDiv" class="fadable"></div> | |
<p> | |
<button id="fadeButton">Fade</button> | |
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var div = document.getElementById("myDiv"); | |
var fadeMeButton = document.getElementById("fadeButton"); | |
fadeMeButton.addEventListener("click", function(e) { | |
div.classList.toggle("fadeout"); | |
}, true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var div = $("#myDiv"); | |
div.position().top; | |
div.position().left; | |
div.width(); | |
div.height(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById("myDiv").getClientRects(); | |
/* | |
[{ | |
bottom: 104, | |
height: 22, | |
left: 10, | |
right: 81, | |
top: 82, | |
width: 71 | |
}] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax({ | |
url: WEBSERVICE_URL, | |
dataType: "json", | |
type: "GET", | |
success: function(response) { | |
var response = JSON.parse(response); | |
console.log(response.time); | |
}, | |
error: function() { | |
// Error handler | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", WEBSERVICE_URL, true); | |
xhr.onreadystatechange = function() { | |
if(xhr.readyState == 4 && xhr.status == 200) { | |
var response = JSON.parse(xhr.response); | |
console.log(response.time); | |
} else { | |
// Error handler | |
} | |
} | |
xhr.send(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myCallback(response) { | |
console.log(response.time); | |
} | |
var url = "http://webservice/time.php?callback=myCallback"; | |
var button = document.getElementById("purejs_time_btn"); | |
button.addEventListener("click", function(e) { | |
var script = document.createElement("script"); | |
script.setAttribute("src", url); | |
document.getElementsByTagName("body")[0].appendChild(script); | |
}, true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("#mylist").delegate("li", "click", function(e) { | |
console.log($(this).text()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var list = document.getElementById(“mylist”); | |
list.addEventListener("click", function(e) { | |
if(e.target.nodeName == "LI") { | |
console.log(e.target.innerText); | |
} | |
}, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment