Off the Ridge code

Off the Ridge Code Newsletter


Starting with jQuery – Part 3

Published April 26, 2014 in jQuery - 0Comments

Click here to download example file

– How to iterate over html nodes:

jQuery provides us with an each function – that can be used to iterate over any collection, whether it’s an object or a simple array. It iterates over DOM elements and passes current loop iteration starting with 0.

.each(function(index, Element))

There are two ways to iterate over the elements:

         alert(index + ‘=’ +$(this).text());


 $(‘div’).each(function(index, elem){
     alert(index + ‘=’ +$(elem).text());}

They both accomplish the same thing, the first one uses $(this), basically passing DOM element to jQuery so this refers to the current element.

–  How to modify DOM:, so to iterate through each div in the DOM tree and update the author node:

$(‘div’).each(function(i){ = “index= ” + i;});

Note: There’s a difference between $(this) and this:
1. $(this) – represents the jQuery object.
2. this – represents the raw javascript (DOM) object.

– How to modify attributes:

We can grab the author attribute using:

var result = $(‘#SomeDiv’).attr(‘author’);

If we want to modify the value of object’s attributes:


   this.title = ‘New Title’;});


     $(this).attr(“title”, ‘New Title’); });

The first example uses raw DOM javascript object (this.title), the second example, while accomplishing the same thing, uses jQuery object and accesses title via jQuery API.

Mousing over reveals the title:

Now, if we want to update multiple values (title, style, etc.) or attributes then we can harness the power of JSON (JavaScript Object Notation) 

  alt: ‘Richard Feynman’,
  width: ‘400’,
 style: ‘border: 1px solid red;’

jQuery supports what’s called a map – which is basically a JSON object with properties.

Note: the attributes above need to match the <img> attributes. 

JSON object is essentially an unordered collection of name/value pairs, self describing, easy to understand AND much faster to parse than XML – and yes, it is language independent.

The first object is delimited by the outside brackets {FirstName, LastName} – then inside we have another object that is part of the outside object – nested object with the Address:
     Street: ‘123 Nice St.’,
     City: ‘Chicago’,
     State: ‘IL’,
     Zip: 60606

You can do arrays: (the square bracket represents an array)
    { “FirstName”:”Jeane” , “LastName”:”Waterman” }, 
    { “FirstName“:”Dania” , “LastName“:”Dates” }, 
    { “FirstName“:”Tamie” , “LastName“:”Rost” }

    title: “Yet Another Title”,
    style: ‘font-size: 20pt; background-color:green;’ 


              title: ‘Yet Another Title’
     .css(‘title’,’Yet Another Title’)
     .css(‘font-size’, ’20pt’);

The second example uses chaining of the jQuery properties.

– How Insert and Remove Nodes:

1. append() – insert content to the end of element.
2. appendTo() – insert element to the end of the target.
3. prepend() – insert content to the beginning of element.
4. prependTo() – insert element to the beginning of the target.
5. remove() – remove elements from the DOM.

.append(‘<span style=”background-color:steelblue”>appended Child 1 Blue</span>’);  

.prepend(‘<span style=”background-color:steelblue”>prepended Child 2 Blue</span>’); 

To remove some rows from the table:


results in:


Click here to download example file.