JavaScript

Sorting an array of objects with the sortBy method of Prototype

Here is an example of how easy it is to sort an array of objects using the Enumerable.sortBy method of the Prototype framework: var obj1 = { lastName : "Lacava", firstName : "Alessandro" }; var obj2 = { lastName : "Brown", firstName : "John" }; var obj3 = { lastName : "Simpson", firstName : "Bart" }; var arr = [obj1, obj2, obj3]; //order by last name var sorted = arr.sortBy(function(obj) { return obj.

How to include a .js file (a JavaScript file) within another .js file

Actually, there is no function you can call to include a JavaScript file within another one. This is the bad news. The good news is that you can use a trick to accomplish this task. Suppose you have two JavaScript files, say file1.js and file2.js. You want to include file1.js within file2.js because you want to use file1’s functions. Here is the code you can use: //this is file2.js document.write("<script src='file1.

How to hide/show an HTML form element depending on a combo box choice

This is an example of how you can show/hide an HTML form element depending on a combo box choice. Put the following JavaScript code between your <head></head> section (or within a .js file if you prefer). <script language="javascript" type="text/javascript"> function hide() { var text = document.formName.textBox; if(document.formName.combo.value == "hide") { text.style.visibility = "hidden"; } else { text.style.visibility = "visible"; } } </script> The following snippet of code instead is the HTML code to use to call the hide function.