March 10th, 2009 at 5:29 pm

JavaScript printf()

The printf function is known in many programming languages. It allows to replace a certain pattern in a string with other strings.

Here's a little printf() for JavaScript. It can't do very much; in fact, it only can replace the placeholder %s multiple times.

JAVASCRIPT:
  1. var printf = function(string)
  2. {
  3.     if (arguments.length <2) { return string; }
  4.         for (var i=1; i<arguments.length; i++)
  5.         { string = string.replace(/%s/, arguments[i]); }
  6.     return string;
  7. }

Although it's not too advanced, it can be helpful if want to produce something as a pagination:

JAVASCRIPT:
  1. var pagination = '';
  2. var numpages = 5;
  3.  
  4. for (var page=1; page<=numpages; page++)
  5. {
  6.     pagination += printf('Page %s of %s', page, numpages);
  7. }

Update 08.04.09, 12:11: Found a better solution.)

February 24th, 2009 at 8:48 pm

Implementing PHP functions in JavaScript

There are some handy functions in PHP you would sometimes like to have in JavaScript, too. Why not reimplement them? It's not hard. Take for example in_array() and explode():

JAVASCRIPT:
  1. function explode(delim, val_to_split)
  2. {
  3.     return (val_to_split.indexOf(delim))
  4.         ? val_to_split.split(delim)
  5.         : [val_to_split];
  6. }
  7.  
  8. function in_array(needle, haystack)
  9. {
  10.     hlength = haystack.length;
  11.     for (var i=0; i<length; i++)
  12.         { if (needle == haystack[i])
  13.             { return true; } }
  14.     return false;
  15. }

Hint: On many pages of PHP functions, there are comments which describe how to reimplement a function (e.g. PHP 4 implementations of PHP 5 functions) -- in PHP though, but it's easy to adapt them for JavaScript.

April 16th, 2008 at 11:20 am

Clear a sized select field

If you have have a select form element with a size attribute, one common option for the user is to leave it empty (especially in combination with multiple). The problem is, as soon as one or more options are selected, it is quite impossible in most browsers, to not select anything at all. And even if there is a way, it is not easy to accomplish. Try for yourself to deselect all items:

With a little JavaScript magic, we can offer a button to clear the select field:

JavaScript:
  1. function nl_clear_select(element)
  2. {
  3.     var opt_length = document.getElementById(element).options.length;
  4.     for (var i = 0; i <opt_length; i++)
  5.         document.getElementById(element).options[i].selected = false;
  6. }

Now try again to clear the select field. Hint: clicking on "Clear selection" will help a lot. ;)

Clear selection

Neat, eh?

April 10th, 2008 at 12:14 pm

Get radio button value in JavaScript

Here's how to get the currently checked value of radio button section. The parameter elementName is the name attribute of the radio input.

JavaScript:
  1. function getRadioValue(elementName)
  2. {
  3.     var element = document.getElementsByName(elementName);
  4.     var bt_count = element.length; // can't use element.length in the loop, as it would decrement
  5.  
  6.     for (var i = 0; i <bt_count; i++)
  7.         if (element[i].checked == true)
  8.             return element[i].value;
  9. }