$('#my_id_or_any_css_selector').on('click',function(){
$(this).css('color','red');
})
document.getElementById('my-button').addEventListener("click", function(){
/* do some stuff (add new stuff to previous click event if exist) */
/* THIS IS THE CURRENT BEST PRACTICES */
},false);
document.getEmentById('my-button').attachEvent('onclick', function(){
/* do some stuff (add new stuff to previous click event if exist) */
/* IE <= 8 */
/* No final boolean parameter to manage bubbuling event */
})
document.getElementById('my-button').onclick=function(){
/* do some stuff (override previous onclick event) */
};
// Do not use this following script if not a good reason (more info)
<a href=”fallback_link.html” onclick=”alert(‘Bad for maintenace’); return false; ”>Alert Me!</a>
<a href=”javascript:alert(‘bad for maintenace and no trigger click event’);”>Alert Me!</a>
$(window).load(function(){
// Do stuff html after the image are loaded
});
window.addEventListener('load', function(){
// Do stuff html load but before image loaded
// Modern browser only
}, false);
$('document').ready(function(){
// Do stuff html load but before image loaded
});
$(function(){
// Do stuff html load but before image loaded
// shorten version
})
jQuery(function($){
// Do stuff html load but before image loaded
// safest version , avoid $ conflic with other library
// or allow using $ when jQuery is in no conflic mode (eg: wordpress)
})
document.addEventListener('DOMContentLoaded', function(){
// Do stuff html load but before image loaded
// Modern browser only
}, false);
document.attachEvent("onreadystatechange", function(){
// Do stuff html load but before image loaded
// old IE browser only
});
$('#container').on('click','.item',function(){
// DO SOMETHING
})
function delegate(elSelector, eventName, selector, fn) {
var element = document.querySelector(elSelector);
if(!element) return;
element.addEventListener(eventName, function(event) {
var possibleTargets = element.querySelectorAll(selector);
var target = event.target;
for (var i = 0, l = possibleTargets.length; i < l; i++) {
var el = target;
var p = possibleTargets[i];
while(el && el !== element) {
if (el === p) {
return fn.call(p, event);
}
el = el.parentNode;
}
}
});
}
// USAGE
delegate('#container', 'click', '.item', function(e) {
// DO SOMETHING
});
$("#my_id").append('<p>hello</p>');
var my_id = document.getElementById('#my_id');
my_id.insertAdjacentHTML('beforeend', '<p>hello</p>');
$("#my_id").prepend('<p>hello</p>');
var my_id = document.getElementById('#my_id');
my_id.insertAdjacentHTML('afterbegin', '<p>hello</p>');
$("#my_id").before('<p>hello</p>');
var my_id = document.getElementById('#my_id');
my_id.insertAdjacentHTML('beforebegin', '<p>hello</p>');
$("#my_id").after('<p>hello</p>');
var my_id = document.getElementById('#my_id');
my_id.insertAdjacentHTML('afterend', '<p>hello</p>');
$('#my_id').empty();
var my_id = document.getElementById('#my_id');
my_id.innerHTML = null;
$(‘#my_id’).parent();
document.getElementById("my_id").parentNode
$(‘#my_id’).children();
document.getElementById("my_id").parentNode.childNodes[]
$(‘#my_id’).next();
var my_id = document.getElementById('#my_id');
my_id.nextElementSibling;
$(‘#my_id’).prev();
var my_id = document.getElementById('#my_id');
my_id.previousElementSibling;
$('my_id').html()
document.getElementById('myid').innerHTML
$('#my_id').text();
document.getElementById('my_id').textContent
$('my_id').replaceWith('my string');
var my_id = document.getElementById('#my_id');
myid.outerHTML = 'my string';
$('#my_id').find('span').hide();
var my_id = document.getElementById('#my_id');
var spans = my_id.querySelectorAll('span');
for(i = 0 ; i< spans.length ; i++ ){
spans[i].style.display = 'none';
}
$('#my_id').addClass('my_class');
var my_id = document.getElementById('#my_id');
my_id.classList.add('my_class');
$('#my_id').removeClass('my_class');
var my_id = document.getElementById('#my_id');
my_id.classList.remove('my_class');
$('#my_id').toggleClass('my_class');
// IE 10 not support
var my_id = document.getElementById('#my_id');
my_id.classList.toggle('my_class');
if($('#my_id').hasClass('my_class')){
// DO SOMETHING
}
if(document.getElementById('my_id').classList.contains('my_class')){
// DO SOMETHING
}
$('#my_id').css('color','red');
var my_id = document.getElementById('#my_id');
my_id.style.color = 'red';
$('#my_id').css('color');
var my_id = document.getElementById('#my_id');
window.getComputedStyle( my_id.getPropertyValue('color')
$('#my_id').show();
var my_id = document.getElementById('#my_id');
my_id.style.display = 'block';
$('#my_id').hide();
var my_id = document.getElementById('#my_id');
my_id.style.display = 'none';
$('#my_button').click(function(){
$('#my_content').fadeOut();
})
function fadeOut(el){
el.style.opacity = 1;
(function fade() {
if ((el.style.opacity -= .1) < 0) {
el.style.display = "none";
} else {
requestAnimationFrame(fade);
}
})();
}
document.getElementById('my_button').onclick = function(){
fadeOut(document.getElementById('my_content'));
return false;
};
$.ajax({
url: "webservice",
type: "POST",
data: "a=1&b=2&c=3",
success: function(d) {
console.log(d);
}
});
var r = new XMLHttpRequest();
r.open("POST", "webservice", true);
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
console.log(r.responseText);
};
r.send("a=1&b=2&c=3");
$.getJSON('http://superapi.org/json', function(data){
console.log(data);
});
function getJSON(path, callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType('application/json');
xmlhttp.onreadystatechange = function() {
ready = (xmlhttp.readyState == 4 && xmlhttp.status == 200);
callback(ready ? xmlhttp.responseText : false);
};
xmlhttp.open('GET', path, true);
xmlhttp.send();
};
getJSON('http://superapi.org/json',function(data){
var data = JSON.parse(data);
console.log(data);
});
$('div').each(function(){
$('this').css({backroundColor : "red"});
});
var nodeList = document.getElementsByTagName('div');
var nodes = Array.prototype.slice.call(nodeList,0); // nodes is an array now.
nodes.forEach(function(node){
node.style.backgroundColor = "red";
});