JavaScript and the DOM series, part 1

JavaScript and the DOM Series: Lesson 1

Feb 20th in JavaScript & AJAX by James Padolsey

Hello and welcome to the first part of what will hopefully be an extensive series covering core aspects of the JavaScript programming language and the DOM API.

While frameworks such as jQuery, Prototype and MooTools are great ways of suppressing browser bugs and speeding up development, it's important to know and understand the underlying technologies. This series intends to tell you what you need to know about JavaScript and the Document Object Model (DOM). Even if you swear by a particular library this will still benefit you - knowing how something works under the skin can only be a good thing!

PG

Author: James Padolsey

I'm a freelance web developer based in Hampton, UK. I write about and enjoy front-end web development. Most of what I write focuses on my favorite topic, JavaScript! To read my blog or find out more about me please visit my site!

Introductions

JavaScript

JavaScript is a dynamic, loosely typed, prototype-based programming language which is used in many different environments. As well as being the prevalent client-side programming language of the web, it's also used to write plugins for IDEs, in PDF files and as a basis for other platforms and higher abstractions.

JavaScript is based on the ECMAScript standard (ECMA-262) and was created by Brendan Eich of Netscape. It was originally called LiveScript but it was later renamed to JavaScript, probably with the sole intention of causing confusion.

Here are some of its features in a little more detail:

  • Dynamic programming languages execute at runtime; they are not compiled. Because of this, JavaScript is sometimes considered a scripting language as opposed to a true programming language (obviously a misconception). When you have JavaScript within an HTML document it will be parsed as the page loads within the browser, hence at "runtime".
  • Loosely typed languages do not insist upon any strong typing system. If you've programmed in C or Java (not the same as JavaScript) you'll know that when declaring a variable you have to specify a type such as 'int' (integer). JavaScript is different in that you don't need to specify the type.
  • To perform inheritance within JavaScript you have to use something called prototypes. JavaScript does not support classes.
  • JavaScript is also a functional language. It treats functions as first-class objects; this is the idea behind lambda.

Understanding the above points is not important in learning about JavaScript; it's just a few ideas to get your brain in gear, and should help you differentiate JavaScript from other programming languages you may have experienced.

Document Object Model

The Document Object Model, normally abbreviated to DOM, is the API through which JavaScript interacts with content within a website. JavaScript and the DOM are usually seen as a single entity since JavaScript is most commonly used for this purpose (interacting with content on the web). The DOM API is used to access, traverse and manipulate HTML and XML documents.

Typical structure of a DOM hierarchy
A basic outline of the typical DOM hierarchy (Simplified)

Here's a few noteworthy things about the DOM:

  • The window object serves as the global object, you access it by just typing "window". It's within this object that all of your JavaScript code is executed. Like all objects it has properties and methods.
    • A property is a variable stored under an object. All variables created on a web-page authomatically become properties of the window object.
    • A method is a function stored under an object. Since all functions are stored under (at least) the window object they can all be referred to as 'methods'.
  • The DOM creates a hierarcy corresponding to the structure of each web document. This hierarchy is made up of nodes. There are several different types of DOM nodes, the most important are 'Element', 'Text' and 'Document'.
    • An 'Element' node represents an element within a page. So if you have a paragraph element ('<p>') then it can be accessed through the DOM as a node.
    • A 'Text' node represents all text (within elements) within a page. So if your paragraph has a bit of text in it can be directly accessed through the DOM.
    • The 'Document' node represents the entire document. (it's the root-node of the DOM hierarchy/tree).
    • Also note that element attributes are DOM nodes themselves.
  • Each layout engine has a slightly different implementation of the DOM standard. For example, the Firefox web browser, which uses the Gecko layout engine, has quite a good implementation (although, not entirely inline with the W3C specification) but Internet Explorer, which uses the Trident layout engine is known for it's buggy and incomplete implementation; a cause of much anguish within the web development community!

Download Firebug

If you're using Firefox and you don't already have the Firebug addon I recomend you download and install it now. It's a very useful tool for getting a decent picture of the entire document structure.

JavaScript on the Web

The Script Element

When you want to use JavaScript on a website it has to be included within a SCRIPT element:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>JavaScript!</title>
    </head>
    <body>
    
        <script type="text/javascript">
        // <![CDATA[
       
        // ]]>
        </script>
    
    </body>
</html>

As you can see we've got a SCRIPT element at the bottom of our document. The TYPE attribute should strictly be set to 'application/javascript' but to no surprise that doesn't work in Internet Explorer so we're stuck with either 'text/javascript' or no TYPE attribute at all. If you care about validation then the former suggestion will probably suit you.

Specify CDATA if you're using XHTML
Remember to specify your scripts as CDATA!

You'll also have noticed that within that SCRIPT element we have a couple of commented-out lines. These inform XHTML-supporting browsers that the content of the SCRIPT element is 'character data' and should not be interpreted as XHTML markup. It's only really necessary if you plan on using either the '<' or '>' characters in your JavaScript code. Obviously you can forget about all this if you're using plain HTML.

The Defer Attribute

Any JavaScript we put inside that SCRIPT element will run as the page loads. The only exception is when a SCRIPT element has a 'defer' attribute. By default, when a browser meets a SCRIPT element, it will stop and run the code, then it will carry on parsing the document. The DEFER attribute informs the browser that the code contains no document-altering code and so can be run later. The only problem with this is that it only works in IE, so it's probably best to avoid this attribute.

Linking to External Scripts

If you want to link to an external script file then simply add an SRC attribute to your SCRIPT element corresponding to its location. It's normally a better idea to have seperate script files than to write code inline as it means the browser can cache the file. Plus you don't need to worry about any of that CDATA nonsense:

<script type="text/javascript" src="my-script.js"></script>

JavaScript essentials

Before we continue with the DOM it's a good idea to have a basic grasp of some JavaScript essentials. If you have trouble understanding some of these, don't worry - you'll eventually pick them up!

In JavaScript you can have different types of values. There are Numbers, Strings, Booleans, Objects, Undefined and Null:

Single-line comments are written using two forward slashes (//), all remaining text on that line is assumed to be a comment by the parser. Multi-line comments are signified using '/*' and '*/' to finish the comment.

Numbers

In JavaScript all numbers are represented as floating-point values. When defining a number variable remember not to wrap it in any quotes.

// Note: ALWAYS use 'var' to declare a variable:
var leftSide = 100;
var topSide = 50;
var areaOfRectangle = leftSide * topSide; // = 5000

Strings

Any string you define is taken literally, JavaScript will not process it. A string is a sequence of Unicode characters and should be wrapped in a matching pair of either single or double quotes.

var firstPart = 'Hello';
var secondPart = 'World!';
var allOfIt = firstPart + ' ' + secondPart; // Hello World!
// The + sign is used as the string-concatenation operator
// (it's also used for numerical addition)

Booleans

Boolean types are useful when you want to evaluate a condition - to see if it meets a specified criteria. There are only two possible Boolean values: true and false. Any comparison, using logical operators, will result in a Boolean.

5 === (3 + 2); // = true 
// You can assign Boolean values to variables:
var veryTired = true;
// You can test for it like this:
if (veryTired) {
	// Sleep
}

The '===' you see above is a comparison operator, we'll cover them later.

Functions

A Function is a specialised Object:

// Using the function operator to create a new function:
function myFunctionName(arg1, arg2) {
	// Function code goes here.
}
    
// If you omit the function's name then
// you're creating an "anonymous function":
function(arg1, arg2) {
	// Function code goes here.
}
        
// Running a function is simply a case of referencing it
// and then adding a parenthesis (with arguments):
myFunctionName(); // No arguments
myFunctionName('foo', 'bar'); // with arguments
    
// You can also run a function without assinging
// it to a variable:
    
(function(){
    // This is known as a self-invoking anonymous function
})();

Arrays

An Array is also a specialised object and can contain any number of data values. To access data values within an array you must use a number, referred to as the 'index' of the item you're trying to retrieve:

// 2 different ways of declaring a new array,
    
// Literal:
var fruit = ['apple', 'lemon', 'banana'];
    
// Using the Array constructor:
var fruit = new Array('apple', 'lemon', 'banana');
    
fruit[0]; // Access the 1st item of the array (apple)
fruit[1]; // Access the 2nd item of the array (lemon)
fruit[2]; // Access the 3rd item of the array (banana)

Objects

An Object is a collection of named values (key - value pairs). It's similar to an array, the only difference is that you can specify a name for each data value.

// 2 different ways of declaring a new Object,
    
// Literal (curly braces):
var profile = {
	name: 'Bob',
    age: 99,
    job: 'Freelance Hitman'
};
    
// Using the Object constructor:
var profile = new Object();
profile.name = 'Bob';
profile.age = 99;
profile.job = 'Freelance Hitman';

If/Else Statement

One of the most common constructs in JavaScript is the 'IF' / 'ELSE' statement. It goes something like this:

var legalDrinkingAge = 21; 
var yourAge = 29;
    
if ( yourAge >= legalDrinkingAge ) {
	// We can use 'alert' to notify the user:
    alert('You can drink.');
} else {
	alert('Sorry, you cannot drink.');
}

JavaScript Operators:

Instead of listing them all out here I suggest you visit the MDC article on Operators. It explains them in a lot of detail. I've setup a few examples to give you an idea of how some of the operators are used below:

// additioa/substraction/multiply/divide
var someMaths = 2 + 3 + 4 - 10 * 100 / 2;
    
// Equality
if ( 2 == (5 - 3 ) { /* Do stuff */ } // == checks for eqaulity
    
// Inequality
if ( 2 != (5 - 3 ) { /* Do stuff */ } 
    
// Strict Equality operators:
// (I suggest using these)
2 === 2 // Instead of 2 == 2
2 !== 3 // Instead of 2 != 3
    
// Assignment:
var numberOfFruit = 9;
numberOfFruit -= 2; // Same as "numberOfFruit = numberOfFruit - 2"
numberOfFruit += 2; // Same as "numberOfFruit = numberOfFruit + 2"

Looping

Looping is useful when you need to go through all items in an array or all members of an object. The most common way to loop in JavaScript is by using either the FOR or WHILE statement.

var envatoTutSites = ['NETTUTS','PSDTUTS','AUDIOTUTS','AETUTS','VECTORTUTS'];
    
// WHILE loop
var counter = 0;
var lengthOfArray = envatoTutSites.length;
while (counter < lengthOfArray) {
    alert(envatoTutSites[counter]);
    counter++; // Same as counter += 1;
}
    
// FOR loop
// (The i stands for "iterator" - you could name it anything)
for (var i = 0, length = envatoTutSites.length; i < length; i++) {
    alert(envatoTutSites[i]);
}
About the FOR loop in JavaScript
FOR loops are more popular for looping through Arrays.

Back to the DOM

Accessing DOM nodes

Let's assume we have a basic XHTML document containing a paragraph and an unordered list:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>JavaScript!</title>
    </head>
    <body>
     
        <p id="intro">My first paragraph...</p>
    
        <ul>
            <li>List item 1</li>
            <li>List item 1</li>
            <li>List item 1</li>
            <li>List item 1</li>
            <li>List item 1</li>
        </ul>
    
        <script type="text/javascript">
        // <![CDATA[
       
        // ]]>
        </script>
    
    </body>
</html>

In this first example we're going to access our paragraph by using the 'getElementById' DOM method:

(This code goes within the SCRIPT element in the above template).

var introParagraph = document.getElementById('intro');
// We now have a reference to the DOM node. This DOM
// node represents the intro paragraph.

The variable 'introParagraph' is now a reference to the DOM node. We can do a number of things with this node, - we can query its content and attributes, and can manipulate any aspect of it. We can remove it, clone it or move it to other parts of the DOM tree.

Anything which is present within a document we can access using JavaScript and the DOM API. So, we might want to access the unordered list in a similar fashion, the only problem is that it doesn't have an ID. You could give it an ID and then use the same method as above or we could access it using 'getElementsByTagName':

var allUnorderedLists = document.getElementsByTagName('ul');
// 'getElementsByTagName' returns a live node collection/list
// - It's very similar to an array with a few slight differences.

getElementsByTagName

The 'getElementsByTagName' method returns a live node collection/list. It's similar to an array in that it has a length property. One important thing to note is these collections are "live" - if you add a new element to the DOM then the collection will update itself. Since it's an array-like object we can access each node via an index, from 0 to the total length of the collection (minus 1):

// Access single unordered list: [0] index
var unorderedList = document.getElementsByTagName('ul')[0];
    
// Create Node list of all list items within the UL:
var allListItems = unorderedList.getElementsByTagName('li');
   
// Now, we can loop through each list item using a FOR loop:
for (var i = 0, length = allListItems.length; i < length; i++) {
    // Extract text node within and alert its content:
    alert( allListItems[i].firstChild.data );
}
Accessing nodes and attributes within the DOM
Accessing nodes and attributes within the DOM

Traversing the DOM

The term "traverse" is used to describe the action of travelling through the DOM, finding nodes. The DOM API gives us plenty of node properties which we can use to move up and down through all the nodes within a document.

These properties are inherent of all nodes and enable you to access related/close nodes:

  • Node.childNodes: You can use this to access all direct child nodes of a single element. It will be an array-like object, which you can loop through. Nodes within this array will include all the different node types including text nodes and other element nodes.
  • Node.firstChild: This is the same as accessing the first item in the 'childNodes' array ('Element.childNodes[0]'). It's just a shortcut.
  • Node.lastChild: This is the same as accessing the last item in the 'childNodes' array ('Element.childNodes[Element.childNodes.length-1]'). It's just a shortcut.
  • Node.parentNode: This gives you access to the parent node of your current node. There will only ever be one parent node. In order to access the grandparent you would simply use 'Node.parentNode.parentNode' etc.
  • Node.nextSibling: This gives you access to the next node on the same level within the DOM tree.
  • Node.previousSibling: This gives you access to the last node on the same level within the DOM tree.
Traversing the DOM
Traversing a document (Simplified - please read below)

So, as you can see, traversing the DOM is incredibly easy, it's just a case of knowing the property names.

One thing to note about the above graphic: the list items can only be retrieved in that fashion if there is no whitespace between them. Because you can have text nodes and element nodes in a document the space in between the '<ul>' and the first '<li>' actually counts as a node itself. Similarily the unordered list is not actually the paragraph's next sibling - since it's on a new line there is space between the two elements - hence, another node! Normally, what you'd do in this situation would be to loop through the 'childNodes' array and test the 'nodeType'. A 'nodeType' of 1 means it's an element, 2 means it's an attribute, 3 means it's a text node. You can see a full list here: https://developer.mozilla.org/En/DOM/Node.nodeType.

That's all it is!

This is how all major JavaScript libraries work behind the scenes; using native DOM methods and properties to give you access to these elements through a nicely polished abstraction. What seperates you from the framework diehards is that you now have some idea of how to survive without a framework (if you didn't already)!

Until Next Time...

Well, that's it for part one. I hope you've learnt something from all my ramblings. In the next part of the series we'll hopefully be focusing on some more applicable examples; we'll probably be covering the browser event model as well.

In the mean time and if you haven't already then check out these talks by Doug Crockford (Yahoo! Video site):

Thanks for reading!

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    Tarek Farage February 20th

    YES!! Thanks for this :)

    ( Reply )
  2. PG

    Drew Douglass February 20th

    Its fantastic to see some pure Javascript articles on here without using a library, great work and keep them coming James!

    ( Reply )
  3. PG

    Yosy February 20th

    Thank You,
    Really Helpful !

    ( Reply )
  4. PG

    M.A.Yoosuf February 20th

    great, but isn’t needed for these days coz, prototype1.6 and jQuery does good job than this right

    am i correct?

    http://yoosuf.awardspace.com/2009/02/simple-jquery-filtering-and-highlight-portfolio/

    ( Reply )
    1. PG

      canciller March 17th

      it´s reeeeeeeeeeeeaaaaaaaally good for me, I think knowing all this stuff can customize even better the frameworks… or create from scracth if you like to write the code your self :) )

      that is my opinion.

      ( Reply )
  5. PG

    Jash Sayani February 20th

    Great tutorial!

    I’d love to see some Python stuff….

    ( Reply )
  6. PG

    Patternhead February 20th

    Angother well explained, helpful tut, thanks :)

    ( Reply )
  7. PG

    danafire February 20th

    Thank you, James Padolsey

    Could you tell me how you made the images. They are nice.

    ( Reply )
  8. PG

    Tuguldur Baatar February 20th

    At a glance it looks very well documented and easy to follow. Will deffinately read this article when I finish work.

    ( Reply )
  9. PG

    gerwin February 20th

    hey great article, excellent coverage of the basics of javascript. thanks for sharing.

    ( Reply )
  10. PG

    xav February 20th

    Sorry, you have an error in your document. It’s not :
    // Equality
    if ( 2 == (5 – 3 ) { /* Do stuff */ } // == checks for eqaulity
    but
    // Equality
    if ( 2 == (5 – 3 ) ) { /* Do stuff */ } // == checks for eqaulity

    Without this, it’s a great tut.
    thanks

    ( Reply )
  11. PG

    Jay El Ehm February 20th

    M.A.Yoosuf:
    What are these librarys (prototype and jQuery) based on? In order to read a book you need to know the letters, right?

    ( Reply )
  12. PG

    Christoph February 20th

    Pls, correct an error – in part “getElementsByTagName”, line 8 should have a semicolon:

    for (var i = 0; length = allListItems.length; i < length; i++) {

    ( Reply )
  13. PG

    KaZ February 20th

    “great, but isn’t needed for these days coz, prototype1.6 and jQuery does good job than this right”

    Doesn’t matter, always good to learn the basics of real javascript.

    ( Reply )
  14. PG

    Mike February 20th

    Excellent and enlightening. Looking forward to the next one. Thanks.

    ( Reply )
  15. PG

    Javier Rios February 20th

    I look forward to reading this tut and the coming ones on JavaScript series. This is a weak area of mine and I want to improve on it. I love the tuts on here and happy this series has begun.

    ( Reply )
  16. PG

    Mariano February 20th

    Excellent article!
    Hopefully this kind of articles will help to make javascript stop being, as Douglas Crockford calls it, the world’s most misunderstood programming language.

    ( Reply )
  17. PG

    ed February 20th

    this is just the kind of thing i need!

    ( Reply )
  18. PG

    acs8369 February 20th

    Thank you, wait to see more.

    ( Reply )
  19. PG

    Shane February 20th

    @M.A.Yoosuf did you not read James’ introduction? He clearly states with good reason that understanding JavaScript is key to fully utilising a JavaScript framework such as jQuery or whatever else is your favourite.

    @James – nice tutorial for JavaScript beginners. Just one thing – how did you do diagrams? They’re very nice.

    ( Reply )
    1. PG

      James March 3rd

      Straight up Photoshop! :D

      ( Reply )
  20. PG

    Hasanga February 20th

    whoa! grate! what a grate way to explain DOM. Those images you have made are really really nice. it’s crystal clear!

    ( Reply )
  21. PG

    Jeffrey Way February 20th

    @M.A.Yoosuf – No! That’s like saying, “I don’t need to learn how to add because I have a calculator.”

    ( Reply )
  22. PG

    Timothy February 20th

    Just a few things:

    You should not use the tag with defer. This is not supported by all browsers. Many browsers will simple execute the script right away rather than after all others. This is a problem with Browser vendors.

    As for objects, you can consider them simple hashes. They don’t work exactly as you would expect a hash to, but they are key / value pairs. This may help if you are used to hashes in other languages.

    And, although it is true that the DOM and JavaScript are often considered one and the same they are really separate entities. JavaScript just gives you an API to access the DOM. the DOM is really just a tree structure of the entire page. And each node on the tree is a JavaScript object containing mixed attributes.

    But, overall, very good article. I must say that it is essential to learn the basic inner-workings of JavaScript before using frameworks like MooTools, Prototype or jQuery. Because when you use a framework it is very useful to understand what the framework is actually doing. When you get into heavy heavy scripting (where scripts can contain 2000+ lines) you will likely have to tweak the framework. Often this is due to bugs you find in the framework, or changes you want to make just to make things less complicated within your own script.

    ( Reply )
  23. PG

    Kai February 20th

    Nice article, hope you delve deeper than this in the next lessons.

    ( Reply )
  24. PG

    David February 20th

    Thanks for this, I’ve been using javascript and jquery for a while now and could hack my way through making changes to other people’s code without knowing a lot of this, but it hasn’t been easy. Keep it coming!

    ( Reply )
  25. PG

    John Dangerous February 20th

    I have no idea what the Document Object Model (DOM) is.
    I have looked it up briefly on a few occasions and haven’t found a quick clear answer. Maybe I will when I watch this vid. Thanks

    ( Reply )
  26. PG

    Joe February 20th

    This is great :D

    ( Reply )
  27. PG

    John Dangerous February 20th

    Good Article.

    ( Reply )
  28. PG

    Brenelz February 20th

    Indeed this is an excellent rundown of JavaScript… once again a great one James!

    ( Reply )
  29. PG

    Braden Keith February 20th

    John I don’t believe this is a vid. lol.

    I have always followed nettuts but never really taken advantage of diligently reading the articles until the past few days. This is some real high quality stuff. I appreciate the explanations and will definitely be bookmarking this one for a later reference!

    ( Reply )
  30. PG

    wayno February 20th

    Great article! The DOM has always been a black box for me. Looking forward to the next part.

    ( Reply )
  31. PG

    José Maria February 20th

    Thanks! two days ago i wondered why not read a tutorial about DOM…well..here it is!

    ( Reply )
  32. PG

    Cody February 20th

    This looks great – I’ve been in search for a good stepping stone in Javascript

    ( Reply )
  33. PG

    Pete February 20th

    Thanks. Really helps to fill in the gaps!

    ( Reply )
  34. PG

    Dennis February 20th

    Thanks for the tut. I like knowing and seeing what is happening in my head when I work. This really helps to visualize the process. Can’t wait for lesson 2

    ( Reply )
  35. PG

    sx February 20th

    Thanks!
    hey, is the object in JavaScript the same as an associative array in PHP?

    It confusing to call it an object when its not oop or based on a class.

    ( Reply )
    1. PG

      James March 3rd

      It’s similar to an associative array in PHP – you can access JavaScript objects using array notation (array[indexOrName]) or dot notation (object.property)

      ( Reply )
  36. PG

    Meshach February 20th

    Wow! Awesome!!!!!

    A+++++++

    ( Reply )
  37. PG

    Terry February 20th

    Thank you!

    ( Reply )
  38. PG

    Chris Paraiso February 20th

    Very nice, in depth. Can’t wait for the next part of the series! Keep it up!

    ( Reply )
  39. PG

    Mojo February 20th

    Great illustrations!

    instead of
    var profile = new Object();

    do
    var profile = {};

    ( Reply )
  40. PG

    Fabian February 20th

    Thanks for the post. It’s really covers the basics well and you explain everything really clearly. Looking forward to the next one :)

    ( Reply )
  41. PG

    Bianca Yvonne February 20th

    Quite helpful for understanding libraries. Thanks!

    ( Reply )
  42. PG

    Chris Gunther February 20th

    Great tutorial. Can’t wait for the rest of the series.

    ( Reply )
  43. PG

    Chris February 20th

    Great Tutorial Series! I’m so glad you started this.

    I hope you continue to break it down dummy-style as you keep this Series going. Thanks!

    oh, and i’m amazed that you’re only 18! Great Job Man!

    ( Reply )
  44. PG

    HamidReza February 20th

    Thank you!

    ( Reply )
  45. PG

    Kapil February 21st

    Thanks a ton, very useful , simple & easy to understand

    ( Reply )
  46. PG

    Lamin Barrow February 21st

    great read. thanx a lot

    ( Reply )
  47. PG

    supralova February 21st

    Wonderful, really helps me

    regards of indonesian

    ( Reply )
  48. PG

    Andy Gongea February 21st

    Now that’s a tutorial. Comprehensive, direct and modular.

    Great work James, thank you for sharing your Javascript knowledge.

    ( Reply )
  49. PG

    Tom February 21st

    thanks for this..

    ( Reply )
  50. PG

    Maicon February 21st

    Very thanks!! A always wanted something like this.

    ( Reply )
  51. PG

    John Kakoulides February 21st

    First off thank you for the article.

    Dynamic programming languages execute at runtime; they are not compiled.

    This is untrue. Some dynamic languages are compiled, like Objective-C.

    Because of this error, the declaration “Dynamic programming languages execute at runtime” is only partially correct. Dynamic languages can defer certain operations until run-time, like extending objects.

    Your description of a dynamic language is closer to the definition of an interpreted language

    ( Reply )
    1. PG

      James March 3rd

      Thank you for the correction :)

      ( Reply )
  52. PG

    Mike February 21st

    Cool tutorial, very informative.

    ( Reply )
  53. PG

    Montana Flynn February 21st

    Very thorough tut! Thanks so much James, you explained it perfectly!

    ( Reply )
  54. PG

    Vahid February 21st

    It’s perfect!
    Thanks.

    ( Reply )
  55. PG

    Jam February 21st

    Very helpful tutorial. I’ve been waiting for something like this!

    ( Reply )
  56. PG

    Eduardo February 21st

    great tutorial!
    javascript rules! It’s important to know that plain javascript is very usefull.

    ( Reply )
  57. PG

    Rocky February 21st

    Yes very nice tutorial.

    ( Reply )
  58. PG

    Raja February 21st

    Woohoo!!!! This the kind of stuff that makes subscribe to you guys.
    Good stuff man

    ( Reply )
  59. PG

    Christopher Kvarme February 22nd

    Clear cut.
    Nice visuals.
    Thanks!!

    ( Reply )
  60. PG

    Kieran Innes February 22nd

    Great tutorial. Thanks.

    ( Reply )
  61. PG

    Elyar February 22nd

    Very Gooood

    ( Reply )
  62. PG

    RACHID February 22nd

    can’t wait till the next lesson !

    ( Reply )
  63. PG

    Winardi February 22nd

    This article is great.

    ( Reply )
  64. PG

    iDevelopThings February 22nd

    Excellent.. very well documented.. Thanks.

    ( Reply )
  65. PG

    Matthias February 22nd

    Thanks a bunch! This is definitely something I needed as I want to learn JavaScript instead of just jumping into a library like jQuery and not really understanding its backgrounds.

    ( Reply )
  66. PG

    Dramoth February 24th

    Great tute… easily understood for even a complete tyro. I am looking forward to some of the more advanced ones.

    For now, I am going to forward this link to a couple of my colleagues so that they can get a start on DOM programming and so they can do something simple with the DOM.

    ( Reply )
  67. PG

    John February 24th

    Great tutorial, I’ve been reading tons of resources about JavaScript for a long time now, and this one was by far the most clear. I look forward to further additions!

    ( Reply )
  68. PG

    Some Funky Dude February 25th

    Great beginner tut on JavaScript and the DOM. What’s good about this article, is the brief overview of the basics, as well as in-depth terminology about the language. I didn’t realize you could create arrays or objects by calling instantiating their object
    e.g. var myArray = new array(’one’, ‘two’);

    ( Reply )
  69. PG

    Sam February 26th

    Thanks for the basic tut. Now that I have a better understanding of plain JavaScript I should be able to use libraries like jQuery more effectively.

    ( Reply )
  70. PG

    Amaiko February 27th

    Waiting for part 2

    ( Reply )
  71. PG

    Andre March 2nd

    I might add: eagerly waiting part 2.
    Excellent tut for beginners(like me).
    Good job dude.

    ( Reply )
  72. PG

    Jeremy March 3rd

    @M.A.Yoosuf,

    JavaScript libraries are still JavaScript, and using any one of them still requires knowledge of the language. So yes, information on JavaScript is still needed.

    ( Reply )
  73. PG

    zaw March 3rd

    very informative thanks for the post and waiting for part 2

    ( Reply )
  74. PG

    David March 8th

    Brilliant Tutorial! Just one question: you talk about document.getElementsByTagName() which

    returns a live node collection/list.
    It’s very similar to an array with a few slight differences.

    What would those difference be?

    ( Reply )
  75. PG

    vbox_ March 10th

    Nice tut James ;)
    By the way, if you guys are interested in manipulating the DOM with JS, DOM Scripting by Jeremy Keith is definitely the book you should to read.

    ( Reply )
  76. PG

    Koistya `Navin March 11th

    Missing brackets in the “JavaScript Operators” code.

    ( Reply )
  77. PG

    Robert Zhou March 22nd

    What program did you use to make the flow chart for

    “A basic outline of the typical DOM hierarchy (Simplified)”?

    They look great!

    ( Reply )
  78. PG

    josheat March 28th

    Awesome post.
    I never bothered looking into the DOM because I could never properly understand it, but this has given me a good understanding of how javascript work.

    Cheers!

    ( Reply )
  79. PG

    CgBaran Tuts April 18th

    Great tutorial thanks

    ( Reply )
  80. PG

    Graphic Designer May 21st

    And so begins my journey into the depths of javascript.

    ( Reply )
  81. PG

    Wookkie September 2nd

    Great Tut for Beginners or to fresh up some skills.
    One thing i like very much at this tut are the great illustrations.
    What App do you use to produce them?

    Thanks for the good work!

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    September 2nd