jQuery-JavaScript: Basic Cheat Sheet for Beginners

on December 20, 2009
(0 minute read)


Check if element exists

if ($('#infobox').length) { /*exists, do things here*/ }

Things I’m buying on Amazon this week

Change attribute

$('#name').attr('title', 'Hello!');

Change content of a container

$('#main').html('<p>Lorem ipsum</p>');

Change page title

document.title = 'Hello!';

Add content before/after

$('table>tbody>tr:last').after('<tr><td>This is now the last row.</td></tr>');


Add or remove classes


Check if element has class

if ($('#main').hasClass('green')) { }


GET with parameters

$.get("page.php", { action: "edit", id: 38 },


Do something with every matching element

  function (intIndex) {
    /* Modify all table rows */


Check if even or odd

if ($id % 2) { /*even*/ }else{ /*odd*/ }

Force string to integer


Please donate to keep this server up

Filter words, numbers or symbols

$(this).attr('id').replace(/[^0-9]+/, '');


setInterval(timer1s, 1000);
function timer1s() { /*run this every second*/ }

Looking for cool gift ideas? TrendLiker.com lists hundreds of nice items and lets you know when they go on sale on Amazon.

Did you enjoy this article?

Grab me a coffee ☕

One comment

Follow me for cool new products and interesting findings on graphic design, web development, marketing, startups, life and humor.

/*Twitter*/ !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs"); /*Facebook (function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) {return;}js = d.createElement(s); js.id = id;js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=28624667607";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));*/ /*Google+*/ window.___gcfg = {lang: 'en-GB'};(function() {var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;po.src = 'https://apis.google.com/js/plusone.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);})();
Airbnb 25€ discount