<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: dynamic code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 19:25:30 GMT</pubDate>
    <description>DZone Snippets: dynamic code</description>
    <item>
      <title>PHP Dynamic Checkbox Table Creator (data retrieved from MySQL DB)</title>
      <link>http://snippets.dzone.com/posts/show/5450</link>
      <description>Hi All.&lt;br /&gt;This is a function for Dynamically Create a Checbox Table retrieving information for from a MySQL Database.&lt;br /&gt;As it is quite commented, it's also good for learning how this things work :)&lt;br /&gt;Hope you find it useful.&lt;br /&gt;Feedback is welcome.&lt;br /&gt;Cheers&lt;br /&gt;Dan&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function dynamic_checkbox_table ($sql_str, $col_label, $col_name, $val_checked="S", $cant_cols_tbl=3){&lt;br /&gt;/*&lt;br /&gt;	by Daniel Neumann&lt;br /&gt;	this script creates dynamically permite a table containing checkboxes &lt;br /&gt;	getting the data for the checkboxes from a MySQL DB&lt;br /&gt;	$sql_str, SQL select string to retrieve data from DB (see example in last comment line)&lt;br /&gt;	$col_label, DB column that has values for the checkbox label &lt;br /&gt;	$col_name, DB column that has values for the checkbox name&lt;br /&gt;	$val_checked="S", value when checked (value="" attribute) it uses the same value for all of them. If you whish to use a dynamic value from a DB, you should comment the line (it&#180;s explained next to the code in the middle of the function) and de-comment the other line (check the code,. you'll understand what I mean). Also, you should use this parameter to specify the column name for the values&lt;br /&gt;	$cant_cols_tbl=3, quantity of columns for the table, it defaults to 3&lt;br /&gt;	usage example: dynamic_checkbox_table("SELECT * FROM keywords", "Keyword", "ID_Keywrd");&lt;br /&gt;*/&lt;br /&gt;	&lt;br /&gt;	//connect DB and run query&lt;br /&gt;	$db="MyDB";&lt;br /&gt;	$db_user="MyUser";&lt;br /&gt;	$pass="MyPass";&lt;br /&gt;	$host="localhost";&lt;br /&gt;	@mysql_connect($host,$db_user,$pass);&lt;br /&gt;	@mysql_select_db($db) or die ("cannot connect to DB");&lt;br /&gt;	$q_resultado = mysql_query($sql_str);&lt;br /&gt;	mysql_close();&lt;br /&gt;	if (mysql_num_rows($q_resultado)==0) exit("no rows returned");&lt;br /&gt;	&lt;br /&gt;	$next_row = mysql_fetch_array($q_resultado); //fetch first row&lt;br /&gt;	&lt;br /&gt;	$output = "&lt;table  border=\"1\"&gt;\n"; //open table tag&lt;br /&gt;	do {&lt;br /&gt;		$output .= "&lt;tr&gt;\n"; //open row tag&lt;br /&gt;		for ($i=1 ; $i &lt;= $cant_cols_tbl ; $i++ ){ //loops as many times as $cant_cols_tbl&lt;br /&gt;			$row=$next_row; //assign $row, next row will be checking next one, that avoids starting a new row when it's gonna be empty&lt;br /&gt;			$output .= "&lt;td&gt;"; //open TD tag&lt;br /&gt;			$output .= (!$row) ? "" : '&lt;input type="checkbox" name="'.$row[$col_name].'" value="'.$val_checked.'" /&gt;'.$row[$col_label]; //create checkbox and data from $row (**** you should comment this line if you whish to use dynamic $val_checked****)&lt;br /&gt;//			echo (!$row) ? "" : '&lt;input type="checkbox" name="'.$row[$col_name].'" value="'.$row[$val_checked].'" /&gt;'.$row[$col_label]; //create checkbox and data from $row (**** you should de-comment this line if you whish to use dynamic $val_checked****)&lt;br /&gt;			$next_row = mysql_fetch_array($q_resultado); //retrieve next row&lt;br /&gt;			$output .= "&lt;/td&gt;\n"; //close TD&lt;br /&gt;		} //close for loop&lt;br /&gt;		$output .= "&lt;/tr&gt;\n"; //close row&lt;br /&gt;	} while ($next_row); //close do-while (and checks if there's another row)&lt;br /&gt;	$output .= "&lt;/table&gt;\n"; //close table&lt;br /&gt;	return $output; &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 May 2008 12:09:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5450</guid>
      <author>dneuma (Daniel Neumann)</author>
    </item>
    <item>
      <title>The eval side of Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5188</link>
      <description>The eval method in Ruby executes the code from within a string. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;eval("puts 'Hello World'")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Hello World&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 29 Feb 2008 13:53:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5188</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Pull JavaScript code dynamically into your web page at run-time.</title>
      <link>http://snippets.dzone.com/posts/show/4911</link>
      <description>Using the JavaScript keyword Eval() with AJAX makes it possible to dynamically add JavaScript code at run-time to your web page.&lt;br /&gt;&lt;br /&gt;Here's the complete code dynalert.html, dynalert.js, and dynalert.cgi&lt;br /&gt;&lt;br /&gt;file:dynalert.html&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"&lt;br /&gt;	"http://www.w3.org/TR/xhtml1-strict.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;&lt;br /&gt;	&lt;head&gt;&lt;br /&gt;		&lt;title&gt;dynalert&lt;/title&gt;&lt;br /&gt;		&lt;meta http-equiv="Content-Type" content="type=text/html; charset=ISO-8859-1" /&gt;&lt;br /&gt;		&lt;script type="text/javascript" src="dynalert.js"&gt;&lt;/script&gt;&lt;br /&gt;	&lt;/head&gt;&lt;br /&gt;&lt;br /&gt;	&lt;body&gt;&lt;br /&gt;&lt;p id="i1"&gt;123 &lt;/p&gt;&lt;input type="button" value="go" onclick="update()" /&gt;&lt;br /&gt;	&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: dynalert.js&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function populatePage(results){&lt;br /&gt;  eval(results);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//send the update&lt;br /&gt;function update() {&lt;br /&gt;&lt;br /&gt;	var strServer = "dynalert.cgi";&lt;br /&gt;	url2 = "";&lt;br /&gt;	SubmitRBData(strServer,"?" + url2);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function SubmitRBData(strUrl, strPostFields) {&lt;br /&gt;	http.open("GET", strUrl + strPostFields, true);&lt;br /&gt;	http.onreadystatechange = handleHttpResponse;&lt;br /&gt;	http.send(null);&lt;br /&gt;}&lt;br /&gt;function handleHttpResponse() {&lt;br /&gt;&lt;br /&gt;	if (http.readyState == 4) {&lt;br /&gt;		if (http.status == 200)&lt;br /&gt;		{&lt;br /&gt;		results = http.responseText;&lt;br /&gt;		populatePage(results); // string response&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function getHTTPObject() {&lt;br /&gt;&lt;br /&gt;	var objXMLHttp=null&lt;br /&gt;&lt;br /&gt;	if (window.XMLHttpRequest)&lt;br /&gt;	{&lt;br /&gt;		objXMLHttp=new XMLHttpRequest()&lt;br /&gt;	}&lt;br /&gt;	else if (window.ActiveXObject)&lt;br /&gt;	{&lt;br /&gt;		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;return objXMLHttp&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;var http = getHTTPObject(); // We create the HTTP Object&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file:dynalert.cgi&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;# dynalert.cgi&lt;br /&gt;&lt;br /&gt;require 'cgi'&lt;br /&gt;&lt;br /&gt;cgi = CGI.new&lt;br /&gt;&lt;br /&gt;#title = cgi['title']&lt;br /&gt;&lt;br /&gt;puts "Content-Type: text/html"&lt;br /&gt;puts&lt;br /&gt;puts "function transcript(){ alert('and Bob said this idea might work.');}"&lt;br /&gt;puts "alert('this is a success');"&lt;br /&gt;puts "alert('we can now pass back anything we like');"&lt;br /&gt;puts "transcript();"&lt;br /&gt;puts "oNew = document.createElement('strong');"&lt;br /&gt;puts "oNew.innerHTML = 'yes - this is bold text';"&lt;br /&gt;puts "document.getElementById('i1').appendChild(oNew);"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 21 Dec 2007 22:36:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4911</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>The 0-1 Knapsack Problem in C</title>
      <link>http://snippets.dzone.com/posts/show/4802</link>
      <description>This is a dynamic-programming algorithm implementation for solving the &lt;a href="http://en.wikipedia.org/wiki/Knapsack_problem"&gt;the 0-1 Knapsack Problem&lt;/a&gt; in C.&lt;br /&gt;&lt;br /&gt;Further explanation is given &lt;a href="http://compprog.wordpress.com/2007/11/20/the-0-1-knapsack-problem/"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;#define MAXWEIGHT 100&lt;br /&gt;&lt;br /&gt;int n = 3; /* The number of objects */&lt;br /&gt;int c[10] = {8, 6, 4}; /* c[i] is the *COST* of the ith object; i.e. what&lt;br /&gt;				YOU PAY to take the object */&lt;br /&gt;int v[10] = {16, 10, 7}; /* v[i] is the *VALUE* of the ith object; i.e.&lt;br /&gt;				what YOU GET for taking the object */&lt;br /&gt;int W = 10; /* The maximum weight you can take */ &lt;br /&gt;&lt;br /&gt;void fill_sack() {&lt;br /&gt;	int a[MAXWEIGHT]; /* a[i] holds the maximum value that can be obtained&lt;br /&gt;				using at most i weight */&lt;br /&gt;	int last_added[MAXWEIGHT]; /* I use this to calculate which object were&lt;br /&gt;					added */&lt;br /&gt;	int i, j;&lt;br /&gt;	int aux;&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt;= W; ++i) {&lt;br /&gt;		a[i] = 0;&lt;br /&gt;		last_added[i] = -1;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	a[0] = 0;&lt;br /&gt;	for (i = 1; i &lt;= W; ++i)&lt;br /&gt;		for (j = 0; j &lt; n; ++j)&lt;br /&gt;			if ((c[j] &lt;= i) &amp;&amp; (a[i] &lt; a[i - c[j]] + v[j])) {&lt;br /&gt;				a[i] = a[i - c[j]] + v[j];&lt;br /&gt;				last_added[i] = j;&lt;br /&gt;			}&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt;= W; ++i)&lt;br /&gt;		if (last_added[i] != -1)&lt;br /&gt;			printf("Weight %d; Benefit: %d; To reach this weight I added object %d (%d$ %dKg) to weight %d.\n", i, a[i], last_added[i] + 1, v[last_added[i]], c[last_added[i]], i - c[last_added[i]]);&lt;br /&gt;		else&lt;br /&gt;			printf("Weight %d; Benefit: 0; Can't reach this exact weight.\n", i);&lt;br /&gt;&lt;br /&gt;	printf("---\n");&lt;br /&gt;&lt;br /&gt;	aux = W;&lt;br /&gt;	while ((aux &gt; 0) &amp;&amp; (last_added[aux] != -1)) {&lt;br /&gt;		printf("Added object %d (%d$ %dKg). Space left: %d\n", last_added[aux] + 1, v[last_added[aux]], c[last_added[aux]], aux - c[last_added[aux]]);&lt;br /&gt;		aux -= c[last_added[aux]];&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	printf("Total value added: %d$\n", a[W]);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	fill_sack();&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 20 Nov 2007 17:17:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4802</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>dynamic rails img headers</title>
      <link>http://snippets.dzone.com/posts/show/4737</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;find views -name [a-z]\*rhtml | xargs -n1 grep -H "@page_title" | grep -v "&lt;%" | sed "s/\@page_title = //" | sed "s/rhtml/png/" | sed "s:views/::" | sed "s:/:_:g" | sed "s:^:public/images/beta/headers/hdr_:" | sed "s/  //g"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 06 Nov 2007 00:52:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4737</guid>
      <author>jm3 (john manoogian III)</author>
    </item>
    <item>
      <title>Add a jar file to Java load path at run time</title>
      <link>http://snippets.dzone.com/posts/show/3574</link>
      <description>Sometimes it is necessary to amend the class load path at run time. For example, dynamically adding jar files containing user-configurable JDBC data sources. The way this is done is truly monstrous -- the URL Path format was only revealed when a colleague looked into the Java library sources.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.net.URL;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.net.URLClassLoader;&lt;br /&gt;import java.net.MalformedURLException;&lt;br /&gt;&lt;br /&gt;public class JarFileLoader extends URLClassLoader&lt;br /&gt;{&lt;br /&gt;    public JarFileLoader (URL[] urls)&lt;br /&gt;    {&lt;br /&gt;        super (urls);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void addFile (String path) throws MalformedURLException&lt;br /&gt;    {&lt;br /&gt;        String urlPath = "jar:file://" + path + "!/";&lt;br /&gt;        addURL (new URL (urlPath));&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static void main (String args [])&lt;br /&gt;    {&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;            System.out.println ("First attempt...");&lt;br /&gt;            Class.forName ("org.gjt.mm.mysql.Driver");&lt;br /&gt;        }&lt;br /&gt;        catch (Exception ex)&lt;br /&gt;        {&lt;br /&gt;            System.out.println ("Failed.");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;            URL urls [] = {};&lt;br /&gt;&lt;br /&gt;            JarFileLoader cl = new JarFileLoader (urls);&lt;br /&gt;            cl.addFile ("/opt/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar");&lt;br /&gt;            System.out.println ("Second attempt...");&lt;br /&gt;            cl.loadClass ("org.gjt.mm.mysql.Driver");&lt;br /&gt;            System.out.println ("Success!");&lt;br /&gt;        }&lt;br /&gt;        catch (Exception ex)&lt;br /&gt;        {&lt;br /&gt;            System.out.println ("Failed.");&lt;br /&gt;            ex.printStackTrace ();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 23 Feb 2007 15:24:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3574</guid>
      <author>mikewilsonuk (Mike Wilson)</author>
    </item>
    <item>
      <title>PHP: Dynamically call a function from list of allowed actions</title>
      <link>http://snippets.dzone.com/posts/show/3234</link>
      <description>This function simply calls another function based on the argument, assuming that argument ($action) is in the "allowed" list of actions.&lt;br /&gt;&lt;br /&gt;settings-config.php&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$settings['actions'] = "list,view,delete,update";&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Catalog-class.php&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public function process_action($action) {&lt;br /&gt;	global $settings;&lt;br /&gt;	&lt;br /&gt;	$allowed_actions = explode(",",$settings['actions']);&lt;br /&gt;	if (is_numeric(array_search($action, $allowed_actions))) {&lt;br /&gt;		$f_name = "product_" . $action;&lt;br /&gt;		$this-&gt;$f_name();&lt;br /&gt;	} else {&lt;br /&gt;		$this-&gt;error_msg = 'ACTION_NOT_ALLOWED';&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;private function product_list() {&lt;br /&gt;}&lt;br /&gt;private function product_view() {&lt;br /&gt;}&lt;br /&gt;private function product_delete() {&lt;br /&gt;}&lt;br /&gt;private function product_update() {&lt;br /&gt;}&lt;br /&gt;private function product_create() {&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Even if product_create() exists, if you do process_action('create'), it will not work... it's basically like a function wrapper.. or something like that...&lt;br /&gt;&lt;br /&gt;UPDATE: Yes I know I could have created an array like &lt;code&gt;$settions['actions'] = array('list','view','delete','update');&lt;/code&gt; but I've modified this code a bit for snippet/display purposes... anyways this is just a reminder for myself, not of actual use or interest for others... comments are welcome though :D&lt;br /&gt;</description>
      <pubDate>Wed, 03 Jan 2007 12:15:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3234</guid>
      <author>alexwilliams (Alex Williams)</author>
    </item>
    <item>
      <title>go from model to associated table name and back</title>
      <link>http://snippets.dzone.com/posts/show/2135</link>
      <description>Given a table object, it returns the related string object; e.g. SubAttribute =&gt; 'sub-attribute'.  Useful if you want to make a list of all your tables with perhaps their fields listed out to the side.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def stringify_table( table, replace_char = '-', pluralize = false )&lt;br /&gt;  string = table.to_s.gsub( /([A-Za-z])([A-Z])/, '\1' &lt;&lt; replace_char.to_s &lt;&lt; '\2' )&lt;br /&gt;  string = string.pluralize if pluralize&lt;br /&gt;  string&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Given a string akin to the name of a table, it returns the related table object; e.g. 'sub_attributes' =&gt; SubAttribute.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def tablify_string( string )&lt;br /&gt;  eval( string.to_s.gsub( /_id/, '' ).singularize.split( '_' ).collect { |word| word.capitalize }.join )&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 03 Jun 2006 00:50:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2135</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>A dynamic form of the class statement.</title>
      <link>http://snippets.dzone.com/posts/show/1708</link>
      <description>These 2 methods are equivalent.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# method 1&lt;br /&gt;class X(object):&lt;br /&gt;    a = 1&lt;br /&gt;&lt;br /&gt;# method 2&lt;br /&gt;X = type('X', (object,), dict(a=1))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 17 Mar 2006 20:21:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1708</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Gmail-Like Incremental Search //JavaScript Class</title>
      <link>http://snippets.dzone.com/posts/show/684</link>
      <description>&lt;a href="http://www.jsfromhell.com/dhtml/incremental-search"&gt;&lt;br /&gt;Implements a GMail-like auto-complete.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;@REQUIRES &lt;a href="http://www.jsfromhell.com/geral/event-listener"&gt;Event-Listener&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//Requires http://www.jsfromhell.com/geral/event-listener&lt;br /&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/dhtml/incremental-search [v1.0]&lt;br /&gt;&lt;br /&gt;IncrementalSearch = function( input, callback, className ){&lt;br /&gt;	var i, o = ( o = this, o.l = [], o.i = input, o.c = null, o.s = { e: null, i: -1 }, o.f = callback || function(){}, o.n = className || "", o );&lt;br /&gt;	for( i in { keydown: 0, focus: 0, blur: 0, keyup: 0, keypress: 0 } )&lt;br /&gt;		addEventListener( o.i, i, function( e ){ o.handler.call( o, e ); } );&lt;br /&gt;};&lt;br /&gt;with( { p: IncrementalSearch.prototype } ){&lt;br /&gt;	(p.constructor.fadeAway = function( o ){&lt;br /&gt;		o instanceof Object ? ( this.trash = this.trash || [] ).push( o ) &amp;&amp; setTimeout( this.fadeAway, 200 ) : arguments.callee.c.trash.pop().hide();&lt;br /&gt;	}).c = p.constructor;&lt;br /&gt;	p.callEvent = function( e ){ this[e] &amp;&amp; this[e].apply( this, [].slice.call( arguments, 1 ) ); };&lt;br /&gt;	p.highlite = function( e ){ ( this.s.e &amp;&amp; ( this.s.e.className = "normal" ), ( this.s = { e: e, i: e.listindex } ).e.className += " highlited", this.callEvent( "onhighlite", this.l[ this.s.i ], this.s.e.d ) ); };&lt;br /&gt;	p.select = function(){ this.s.i + 1 &amp;&amp; ( this.i.value = this.l[ this.s.i ], this.callEvent( "onselect", this.i.value, this.s.e.d ), this.hide() ); };&lt;br /&gt;	p.hide = function(){ ( this.c &amp;&amp; this.c.parentNode.removeChild( this.c ), this.c = null, this.l = [], this.s = { e: null, i: -1 }, this.callEvent( "onhide" ) ); };&lt;br /&gt;	p.next = function(){ var e = ( e = this.s.e ) ? e.nextSibling || e.parentNode.firstChild : null; e &amp;&amp; this.highlite( e ); };&lt;br /&gt;	p.previous = function(){ var e = ( e = this.s.e ) ? e.previousSibling || e.parentNode.lastChild : null; e &amp;&amp; this.highlite( e ); };&lt;br /&gt;	p.handler = function( evt ){&lt;br /&gt;		var o = this, t = evt.type, k = evt.key, e = /span/i.test( ( e = evt.target ).tagName ) ? e.parentNode : e;&lt;br /&gt;		t == "keyup" ? k != 40 &amp;&amp; k != 38 &amp;&amp; k != 13 &amp;&amp; o.show()&lt;br /&gt;		: t == "keydown" ? ( k == 40 &amp;&amp; o.next() ) || ( k == 38 &amp;&amp; o.previous() )&lt;br /&gt;		: t == "keypress" ? k == 13 &amp;&amp; !evt.preventDefault() &amp;&amp; o.select()&lt;br /&gt;		: t == "blur" ? o.constructor.fadeAway( o )&lt;br /&gt;		: t == "click" ? o.select()&lt;br /&gt;		: t == "focus" ? o.show()&lt;br /&gt;		: o.highlite( e );&lt;br /&gt;	};&lt;br /&gt;	p.show = function(){&lt;br /&gt;		var cS, found = 0, o = this, i = o.i, iV = i.value, d = document, c = ( o.hide(), o.c = d.body.appendChild( d.createElement( "div" ) ) );&lt;br /&gt;		( c.className = o.n, cS = c.style, cS.display = "none", cS.position = "absolute", o.callEvent( "onshow" ) );&lt;br /&gt;		o.f.call( function( s, x, data ){&lt;br /&gt;			if( !( x.length == undefined ? ( x = [x] ) : x ).length )&lt;br /&gt;				return;&lt;br /&gt;			var j, l = 0, i = o.l.length, e = c.appendChild( d.createElement( "div" ) );&lt;br /&gt;			for( j in ( o.l[i] = s, e.className = "normal", e.d = data, e.listindex = i, !found &amp;&amp; i == o.s.i &amp;&amp; ++found &amp;&amp; o.highlite( e ), x ) )&lt;br /&gt;				e.appendChild( d.createTextNode( s.substring( l, x[j] ) ) ).parentNode.appendChild( d.createElement( "span" ) ).appendChild( d.createTextNode( s.substring( x[j], l = x[j] + iV.length ) ) ).parentNode.className = "selectedText";&lt;br /&gt;			for( x in ( e.appendChild( d.createTextNode( s.substr( l ) ) ), { click: 0, mouseover: 0 } ) )&lt;br /&gt;				addEventListener( e, x, function( e ){ o.handler.call( o, e ); } );&lt;br /&gt;		}, iV );&lt;br /&gt;		if( !c.childNodes.length )&lt;br /&gt;			return o.hide();&lt;br /&gt;		for( var x = i.offsetLeft, y = i.offsetTop + i.offsetHeight; i = i.offsetParent; x += i.offsetLeft, y += i.offsetTop );&lt;br /&gt;		( cS.display = "block", cS.left = x + "px", cS.top = y + "px", !found &amp;&amp; o.highlite( c.firstChild ) );&lt;br /&gt;	};&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;/*container da lista*/&lt;br /&gt;.autocomplete{&lt;br /&gt;	cursor: pointer;&lt;br /&gt;	border: 1px solid #999;&lt;br /&gt;	border-top: none;&lt;br /&gt;	background: #eee;&lt;br /&gt;}&lt;br /&gt;/*caracteres que combinaram*/&lt;br /&gt;.autocomplete .selectedText{ font-weight: bold; color: #008; }&lt;br /&gt;/*items n&#227;o selecionados*/&lt;br /&gt;.autocomplete .normal{ border-top: 1px solid #999; overflow: hidden; white-space: pre; }&lt;br /&gt;/*item selecionado*/&lt;br /&gt;.autocomplete .highlited{ background: #ddf; }&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;form action=""&gt;&lt;br /&gt;	&lt;fieldset&gt;&lt;br /&gt;		&lt;legend&gt;Preenchimento din&#226;mico&lt;/legend&gt;&lt;br /&gt;		&lt;label for="list" &gt;Emails&lt;/label&gt;&lt;br /&gt;		&lt;input autocomplete="0" type="text" name="list" id="list" /&gt;&lt;br /&gt;		&lt;br /&gt;&lt;br /&gt;		&lt;label for="ip" &gt;Lista de IPs&lt;/label&gt;&lt;br /&gt;		&lt;textarea name="ip" rows="3" cols="20" id="ip"&gt;&lt;/textarea&gt;&lt;br /&gt;		&lt;br /&gt;&lt;br /&gt;	&lt;/fieldset&gt;&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;//&lt;![CDATA[&lt;br /&gt;&lt;br /&gt;var list = [ "192.168.0.1", "192.168.0.2", "192.168.0.3", "192.168.1.1", "192.168.1.2", "192.168.1.3", "200.168.0.1", "200.168.0.2", "200.168.0.3", "200.168.1.1", "200.168.1.2", "200.168.1.3" ];&lt;br /&gt;new IncrementalSearch( document.forms[0].ip, function( search ){&lt;br /&gt;	for( var i in list )&lt;br /&gt;		if( !list[i].indexOf( search ) )&lt;br /&gt;			this( list[i], 0 );&lt;br /&gt;}, "autocomplete" );&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;var names = [ "Jo&#227;o Alves &lt;joao@123.com&gt;", "Jonas Raoni Soares Silva &lt;jonas@abc.com&gt;", "Roberto &lt;rob@net.net&gt;", "Maria Fernanda &lt;mariaf@i.tu&gt;" ];&lt;br /&gt;&lt;br /&gt;function retrieveNames( search ){&lt;br /&gt;	search = search.toLowerCase();&lt;br /&gt;	for( var i in names ){&lt;br /&gt;		if( search ){&lt;br /&gt;			for( var j = 0, indices = []; j = names[i].toLowerCase().indexOf( search, j ) + 1; indices[indices.length] = j - 1 );&lt;br /&gt;			this( names[i], indices, i );&lt;br /&gt;		}&lt;br /&gt;		else&lt;br /&gt;			this( names[i], 0, i );&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;x = new IncrementalSearch( document.forms[0].list, retrieveNames, "autocomplete" );&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//]]&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 09 Sep 2005 07:41:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/684</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
