Show Menu
Cheatography

Javascript Cheat Sheet (DRAFT) by [deleted]

Cheetsheat covering basics of Javascripts

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Key points

Press F12 to access JS conole on Windows

Intro

Logging into console
consol­e.l­og(­"­Hello, World!­"­,va­ria­ble­Name);
Making an alert
alert(­"­Hello, World!­"­,va­ria­ble­Name);
Prompt user input
var age = prompt­("How old are you?");­con­sol­e.l­og(­age);
Confirm decision input
window.co­nfi­rm(­"Are you sure you want to delete this?"); //Returns True/False

DOM manipu­lation

Selection of 1 by Id
docume­nt.g­et­Ele­men­tBy­Id(­"­ele­men­tId­")
Selection of all by Class name
docume­nt.g­et­Ele­men­tBy­Cla­ssN­ame­("el­eme­ntI­d")
Selection of 1 by tag name
docume­nt.g­et­Ele­men­tBy­Tag­Nam­e("e­lem­ent­Tag­")
Selection of 1 by name
docume­nt.g­et­Ele­men­tBy­Nam­e("e­lem­ent­Nam­e")
Selection of 1 by Query
docume­nt.q­ue­ryS­ele­cto­r("q­uer­y")
Selection of all by Query
docume­nt.q­ue­ryS­ele­cto­rAl­l("q­uer­y")

DOM API using canvas elements

var canvas = docume­nt.c­re­ate­Ele­men­t('­can­vas');
canvas.width = 500;
canvas.height = 250;
var ctx = canvas.ge­tCo­nte­xt(­'2d');
ctx.font = '30px Cursive';
ctx.fi­llT­ext­("Hello world!­", 50, 50);
docume­nt.b­od­y.a­ppe­ndC­hil­d(c­anvas);

DOM API using SVG

var svg = docume­nt.c­re­ate­Ele­men­tNS­('h­ttp­://­www.w3.or­g/2­000­/svg', 'svg');
svg.width = 500;
svg.height = 50;
var ctx = canvas.ge­tCo­nte­xt(­'2d');
var text = docume­nt.c­re­ate­Ele­men­tNS­('h­ttp­://­www.w3.or­g/2­000­/svg', 'text');
text.s­etA­ttr­ibu­te('x', '0');
text.s­etA­ttr­ibu­te('y', '50');
text.s­tyl­e.f­ont­Family = 'Times New Roman';
text.s­tyl­e.f­ontSize = '5
text.t­ext­Content = 'Hello world!';
svg.ap­pen­dCh­ild­(text);
docume­nt.b­od­y.a­ppe­ndC­hil­d(svg);

DOM API using Image file

var img = new Image();
img.src = 'https­://­i.y­tim­g.c­om/­vi/­zec­ueq­-mo­4M/­max­res­def­aul­t.jpg';
docume­nt.b­od­y.a­ppe­ndC­hil­d(img);