Archive for HTML/CSS

Toggle Text Field Values with jQuery

I saw this post on the CSS Tricks Snippet Feed which addresses a commonly desired form behavior: to provide default text in an INPUT element that disappears when the user enters that field. The example provided works, but I have a couple issues with it:

  1. Uses inline JavaScript, and must be reapplied to each element affected
  2. Repeats the contents of the value attribute
  3. The default text is not replaced if the user exits the field without entering new data

So I created a possible solution, using jQuery:

HTML

1
<input type="text" value="Place default text here" />

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
jQuery( function(){
  $( 'input[type="text"]' ).each( function(){
    $(this).attr( 'title', $(this).val() )
      .focus( function(){
        if ( $(this).val() == $(this).attr('title') ) {
          $(this).val( '' );
        }
      } ).blur( function(){
        if ( $(this).val() == '' || $(this).val() == ' ' ) {
          $(this).val( $(this).attr('title') );
        }
      } );
  } );
} );

This script first iterates over each of the text input to assign a semantic text attribute, helpful not only in storing the default value, but also providing a tool-tip for reference as the user interacts with the page. It then assigns behaviors for both the onfocus and onblur events, eliminating the need to respecify data for comparison. The script is cleanly separated from the markup and, using jQuery, additional specifications may be made so as to only affect children of a particular FIELDSET or only those that possess a certain class.

I hope you find this snippet useful, and feel free to comment if you have additional information to share.

Comments

Visualize Your Markup with Processing

I recently discovered an interesting applet developed by Aharef in 2006 that uses the Processing programming language to graph the element tree of a given web page.

For instance the current markup on this site gets rendered like this:

An HTML DOM Visualizer Applet
Graph of http://blog.paulgueller.com

According to the documentation and examples, the colors represent:

  • blue: links (the A tag)
  • red: tables (TABLE, TR and TD tags)
  • greenDIV tags
  • violet:  images (the IMG tag)
  • yellow: forms (FORM, INPUT, TEXTAREA, SELECT and OPTION tags)
  • orange: linebreaks and blockquotes (BR, P, and BLOCKQUOTE tags)
  • black: the HTML tag, the root node
  • gray: all other tags

Processing is an open-source programming language and development environment used for prototyping and producing images, animation, and interactions within a visual context. The code-graphing applet requires Java, but processing.js has made the language more widely accessible and available for experimentation.

Comments