Novice

How to remove content

Removing a page element is very easy, once you have a reference to it. Use the standard methods for getting the reference (jQuery selectors, etc.) then use the .remove() method.

$("#myUnWantedDIV").remove();

This code would remove the referenced element (if found), and everything within the element. Basically, that node is removed from the DOM tree.

You can get a little fancier by using the jQuery manipulation tags. For instance, to remove everything in the parent of the target element, we could do this:

$("#myUnwantedElement").parent().remove();

Datepicker

The UI Datepicker plugin provides a dynamic and handy popup calendar for selecting dates. It is very flexible and can be configured and themed in different ways.

The definitive sample of what can be done with the Datepicker can be found at the official documentation or at the demo site.

How to work with Ajax (overview)

Asynchronous JavaScript and XML (aka Ajax) is a great technique for refreshing your web page without forcing a full reload of the page. It allows developers to ask for only pertinent parts of the page, resulting in a much more responsive web site. But working with Ajax can be challenging for those just getting started.

Anonymous functions (aka inline functions)

When working with JavaScript Object Notation, we sometimes want to create a function (or method if we use the OOP terminology). The traditional method of creating a function will not work well. There are a couple of ways to create methods in this situation, but we'll focus on the one jQuery favors - anonymous functions.

JavaScript Object Notation

JavaScript provides some short hand ways to create custom objects and arrays. These short hand methods are very useful when dealing with custom objects or needing to define objects and behavior from a string (such as when using Ajax).

JavaScript Object Notation is what we are referring to. However you may see this more commonly referred to as "JSON".

Arrays

These two lines are identical

var array_one = new Array();
var array_two = [];

Check if it exists

Perhaps you have a situation where you declare a person object that may or may not get a "birthday" property declared, but we want processing to continue regardless. Outputting the properties and their values is great for us Humans, but the code cannot "see" missing items. However in code we can instead just check if the property exists.

Keep in mind that JavaScript considers anything that is not zero, null, or undefined to be equivalent to a positive value. So, we can write code like this:

if (myObject.birthdate) {
alert(myObject.birthdate);
}

Check the properties

Sometimes we have an object and the object is valid, but the property we are trying to check is not. We could do an alert for every property, but this could be time consuming. Sometimes a simple list of properties and their values is what we need. You can do this with a quick routine:

var output = "";
for (var prop in myObject) {
output = output + prop + "; " + myObject[prop] + "\n";
}
alert(output);

Dump your variables

When you know what line is causing the error, but you cannot see an immediate reason for the error, dump all the variables that are involved. When we say "dump", we simply mean to show the current content or value of the variables. For instance, if we were not getting into a loop, we might do this:

alert("myArray.length: " + myArray.length);
for (var x=0; x < myArray.length; x++) {
alert("x: " + x);
test.innerHTML = test.innerHTML + "" + myArray[x] + "

Manual Debugging

If you find yourself working without a JavaScript console, or the reported error message just isn't quite right, doing some manual debugging is needed. This could be simply determining WHERE the error occurs, or it could be determining WHY the error occurs. Knowing where and why normally leads directly to the needed fixes.

How to troubleshoot JavaScript

Being able to properly identify a bug, and determine where it is in the code, is a definite skill when it comes to writing ANY code. But JavaScript does not have the same legacy as languages like C/C++, Perl, PHP, etc. We are highly dependent on what the browser can tell us, or an external script debugger. So debugging JavaScript is a little different than these languages.

What follows are recommendations. All web developers follow these to some degree or another, but these tips will help track down any JavaScript code issues quickly.