? Earlier 4 items total Later ?

On this page:?

Selecting and checking items from HTML menus

This code allows you to pass in an HTML menu (select, radio, or checkbox) along with a single choice or an array of choices to be selected. The choice(s) should be contained literally in the value attribute.

This is handy for making persistent forms without polluting the HTML with PHP logic. For example, you could initialize all your menus to have nothing selected and then pass in GET or POST variables in case the form was not accepted, easily making form selections persistent.

/**
 * For select menus.
 */
function select_from_menu($menu,$choices,$deselect=true) {
  if ($deselect) $menu = preg_replace('/ selected(="selected")?/','',$menu);
  if(!is_array($choices)) {
    $menu = preg_replace('/(value="'.preg_quote($choices).'")/',"$1 selected=\"selected\"",$menu);
  } else foreach($choices as $value) {
    $menu = preg_replace('/(value="'.preg_quote($value).'")/',"$1 selected=\"selected\"",$menu);
  }
  
  return $menu;
}

/**
 * For radio buttons and checkboxes.
 */
function check_from_menu($menu,$choices,$deselect=true) {
  if ($deselect) $menu = preg_replace('/ checked(="checked")?/','',$menu);
  if(!is_array($choices)) {
    $menu = preg_replace('/(value="'.preg_quote($choices).'")/',"$1 checked=\"checked\"",$menu);
  } else foreach($choices as $value) {
    $menu = preg_replace('/(value="'.preg_quote($value).'")/',"$1 checked=\"checked\"",$menu);
  }

  return $menu;
}

Clear form with Javascript

Purpose: Clear all text fields in a form

Other important features:
- Resulting page must be valid XHTML 1.1
- JS must work in Firefox 1.0 and MS Internet Explorer 6.0
- JS must never generate any errors, even non-fatal ones, in the Firefox Javascript Console
- JS should only clear text input (no hidden fields or visual controls)
- Code must be readable and properly indented

Looking around the web, this was not easy to find. After a few hours of searches, and some back'n'forth at Webmaster World, I got a working version.

In XHTML header:


In html/form/div


In clear-form-input.js:

function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

        if (document.getElementsByTagName)
        {
                elements = form.getElementsByTagName('input');
                for( i=0, elm; elm=elements.item(i++); )
                {
                        if (elm.getAttribute('type') == "text")
                        {
                                elm.value = '';
                        }
                }
        }

        // Actually looking through more elements here
        // but the result is the same.
        else
        {
                elements = form.elements;
                for( i=0, elm; elm=elements[i++]; )
                {
                        if (elm.type == "text")
                        {
                                elm.value ='';
                        }
                }
        }
}
        
function addEvent(elm, strEvent, fnHandler)
{
        return ( elm.addEventListener
        ? elm.addEventListener( strEvent, fnHandler, false)
        : elm.attachEvent( 'on'+strEvent, fnHandler)
        );
}

Redirect after 5 seconds



  Redireccionado 
  


Bla, bla, bla,...

Rounded Mac Input Box

This html code makes an input box that looks like the one found at here (Under the search downloads box) It works only on macs(safari tiger only i believe) and does not validate. Note: type="search" is what makes it work.

? Earlier 4 items total Later ?