Old but cool HTML tags

I discovered three new html tags that are actually quite old, I just hadn't heard of them.

The first one is <fieldset>. You use fieldset to arrange your form elements together. It creates a box around the elements. It's good for accessibility because it groups elements together into semantically obvious groups. See http://www.htmlcodetutorial.com/forms/_LEGEND.html You can use the <legend> tag to put a title into the box as well.

The second one is <optgroup> it is used to group together elements in a drop down menu. See http://www.w3schools.com/tags/tag_optgroup.asp It's really useful when you want to put headings inside your drop down menu so you can create semantic (that word again!) groups. I used to do this by putting in a option and then using PHP to check that the user didn't try and select that one. This way they can't select the heading! Also note that technically this is supposed to create a nested menu but it only creates a heading. Ironically only IE for Mac follows the standard correctly.

This php code will create an drop menu using optgroup:

function optionListWithGroup($name, $options, $default) {
 // $options[<groupname>][] = array(<fieldname> =>
<value>);
 if ($options AND $name) {
  print('<select name="'.$name.'" >');

  foreach ($options as $group=>$info) {
   
   echo '<optgroup label = "'.$group.'">';
   
   foreach ($info as $data){
    foreach ($data as $key=>$value){
    
     echo '<option';
     echo ' value = "'.$value.'"';
     if (strtoupper($value) == strtoupper($default)) {
      print(' selected');
     }
     print('>');
     print($key);
     print('</option>');
    
    }
   }
   echo '</optgroup>';
  }
  print('</select>');
 }
}

And last is a php function I found which is using for search fields - metaphone(). You give metaphone a string and it returns the phonetic expression of the string. Then using levenshtein() you can determine the 'distance' of two metaphone strings. The distance will tell you how similar the words are. You can use this to implement a 'Did you mean' or similar function in your search pages.

Well that ends my geek code sharing!

Comments

Submitted by Joelith on Tue 08/08/2006 - 10:03

Whoops. The comment in the php code is old and incorrect!. To use the function you would do this:

$options[<groupname>][] = array(<fieldname>=><value>);