Archive for April, 2008

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. }

April 3rd, 2008 at 12:06 pm

Calculate interest rates with a shell script

If you put your cash to a bank account, you will want to know how much money you get with a certain amount at a given rate within a given period. Save the following Bash script as /usr/local/bin/loancalc (or something like that), make it executable, and you will be able to calculate the return on your investments with a single one-liner.

CODE:
  1. #!/bin/bash
  2.  
  3. function calc()
  4. {
  5.     return $(echo "scale=$2; $1" | bc)
  6. }
  7.  
  8. if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
  9.     echo "$0 calculates the interest loan of a given amount with a given rate for a given duration."
  10.     echo "Usage: $0 AMOUNT RATE DURATION"
  11.     exit 0
  12. fi
  13.  
  14. if [ -z "$3" ]; then
  15.     echo "Please enter the amount, the rate and the duration (in this order)."
  16.     exit 1
  17. fi
  18.  
  19. AMOUNT=$1
  20. ZINS=$2
  21. DURATION=$3
  22.  
  23. echo "Investment: $AMOUNT bucks at a rate of $ZINS% for $DURATION years."
  24. echo "          Loan            Total"
  25.  
  26. for i in $(seq 1 $DURATION); do
  27.     INTERESTLOAN=$(echo "scale=10; ($AMOUNT/100)*$ZINS" | bc)
  28.     AMOUNT=$(echo "scale=10; $AMOUNT+$INTERESTLOAN" | bc)
  29.     printf "Year ${i} %8.2f " $INTERESTLOAN; printf " %14.2f\n" $AMOUNT;
  30. done
  31.  
  32. printf "Final amount: %1.2f bucks.\n" $AMOUNT

For example:

CODE:
  1. you@yourmachine ~ $ loancalc 3000 3.5 5
  2. Investment: 3000 bucks at a rate of 3.5% for 5 years.
  3.           Loan            Total
  4. Year 1   105.00         3105.00
  5. Year 2   108.68         3213.68
  6. Year 3   112.48         3326.15
  7. Year 4   116.42         3442.57
  8. Year 5   120.49         3563.06
  9. Final amount: 3563.06 bucks.