Mar 10 2010

How to write a text description into html input type password

Category: JavascriptSimone Lippolis @ 11:58 am

Sometime designers put form labels and instrucions into html inputs. One of the common uses is for login boxes when there’s not much space.

An example of login form design

An example of login form design

In this case, you have to choose between:

  1. Set the password field as type=”text”, thus not masking users’ passwords;
  2. Set the password field as type=”password”, thus not letting the user see what’s written inside the form field.

To avoid this problems, I wrote a small jQuery plugin which dynamically replaces selected <input type=”password” /> fields with <input type=”text” /> fields displaying a chosen value. The replace procedure works forth and back: when the user clicks on the input field it is transformed into a <input type=”password” />; if the user inserts some text, then the field remains masked, if he/she does not insert anything, the field is replaced again, letting the chosen value be visible again.

To use it, simply select the form element you want to replace.It works with any input type=”password” on the page, it can replace a single element, each element with a specific class, or property, or every password element.

// Replaces each &lt;input type="password" /&gt; element
$(document).ready(function () {
 $('input[type=password]').txReplaceFormPassword({
 show_text : 'Password'
 });
 });
// Replaces the &lt;input type="password" /&gt; element with id="pwd"
$(document).ready(function () {
 $('#pwd').txReplaceFormPassword({
 show_text : 'Password'
 });
 });
// Replaces each &lt;input type="password" /&gt; element with class="password"
$(document).ready(function () {
 $('.password').txReplaceFormPassword({
 show_text : 'Password'
 });
 });

Here is the complete code:

/***********************************************************
* txReplaceFormPassword.js
*
* Written by Simone Lippolis - http://simonelippolis.com
*
* This script is distributed under a Creative Commons - Commercial, Attribution licence
* http://creativecommons.org/licenses/by/2.5/it/
*
* Feel free to copy, edit, redistribute this script. Please, don't remove credits.
*
* If you find some bug or error, please let me know.
*
* How to use:
*
* 1. Include jquery:
* &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
*
* 2. Include this script:
* &lt;script type="text/javascript" src="txReplaceFormPassword.js"&gt;&lt;/script&gt;
*
* 3. On document ready, invoke the plugin
*
* $(document).ready(function () {
*         $('#pwd').txReplaceFormPassword({
*            show_text : 'Password'
*        });
*     });
*
* consider '#pwd' as the object which needs to be replaced:
*
* &lt;form action="someaction" method="post" id="formid" name="formname"&gt;
*        &lt;input type="password" name="pwd" id="pwd" value="" /&gt;
*    &lt;/form&gt;
*
* The 'show_text' parameters contains the text to be shown in password's
* text field.
*
***********************************************************/

jQuery.fn.txReplaceFormPassword = function(options) {
 var options = jQuery.extend( {
 show_text: 'Password'
 },options);

 function trim(txt) {
 return txt.replace(/^\s+|\s+$/g, '');
 }

 return this.each(function() {
 if (jQuery(this).val().length == 0) {

 var $pos = jQuery(this).position();
 var $style = jQuery(this).attr('style');
 var $class = jQuery(this).attr('class');
 var $size = jQuery(this).attr('size');
 var $id = jQuery(this).attr('id');
 var $name = jQuery(this).attr('name');

 jQuery(this).hide();
 jQuery(this).after('&lt;input type="text" id="txgrpl-' + $id + '" name="txgrpl-' + $name + '" size="' + $size + '" style="' + $style + '" value="' + options.show_text + '" rel="' + $id + '" title="' + options.show_text + '" /&gt;');

 $('#txgrpl-' + $id).focus(function() {
 $(this).hide();
 var $id = $(this).attr('rel');
 $('#' + $id).show().focus();
 });
 }

 jQuery(this).blur(function() {
 if (trim(jQuery(this).val()) == '' || jQuery(this).val() == undefined) {
 var $id = jQuery(this).attr('id');
 jQuery(this).hide();
 $('#txgrpl-' + $id).show();
 }
 });
 });
};
Share

Tags: , ,


Dec 13 2009

Mini gallery/slideshow with PHP and JQuery

Category: Javascript,PhpGiulio Pons @ 1:52 pm

Sometimes I’ve had to quickly put in a page a simple slideshow, the first times I’ve searched a lot around for a small gallery, but finally I’ve decided that the best way is to do it by myself. This because every complex and very nice gallery that I’ve found need to be studied to make them work as you want, and sometimes they have complex css to integrate or php code to put in the correct directory… I think that many times we all just need a simple php that read a dir, and pass images array to a stupid slideshow.

If you already have JQuery in your pages this gallery is really fast, it works with this small PHP function that find images:

/*
    this php function read all the jpg/gif/png files in the specifed
    directory and return a string with the names
*/
function getJsArray($dir) {
	$out = "";
	if ($dh = opendir($dir)) {
		while (($file = readdir($dh)) !== false)
			if(in_array( substr(strrchr($file, '.'), 1) , array('gif','png','jpg') ))
				$out.= ($out?",":"")."'".$dir.$file."'";
		closedir($dh);
	} else { die ("no dir"); }
	return $out;
}

And uses this tiny javascript (the first lines are for configuration, they also call the php function on the server to read images files):

//---------------------------------------------
var gallery_current_pos = 0;			// gallery counter position
var gallery_idname = "container";		// id of the gallery container
var gallery_timer = 5000;			// 5 seconds
var gallery_ar = Array(<?=getJsArray("./minislides/")?>);

function goGallery() {
	if (gallery_current_pos>gallery_ar.length - 1) gallery_current_pos =0;
	$('#'+gallery_idname).fadeTo("fast", 0 , function () {
		$('#'+gallery_idname).css("background-image","url(" + gallery_ar[gallery_current_pos] + ")");
		$('#'+gallery_idname).fadeTo("slow", 1);
		gallery_current_pos++;
	});
	if (gallery_ar.length>1) setTimeout(	function () { goGallery(); }, gallery_timer);
}
//---------------------------------------------

The “container” which will show the images is simply a “div” with some defined dimensions declared with css, and the javascript will change its background to show images.

When the page is loaded simply call the function to start gallery.

Watch demo. Watch source.

Share

Tags: , ,


Nov 17 2009

Set “write here” on input type text?

Category: JavascriptGiulio Pons @ 2:32 pm

If you are using JQuery framework and you want to set up the default value of some text box in a form you can use this function that defines the onclick function, the onblur function and set the default value and css.
The configuration of the input type text box is defined into the rel attribute, here is the first part, the html code:

<style>
input.csson { color: red; }
input.cssoff { color: gray; }
</style>
<form>
<input id='author' type='text' value='' rel="writehere|type author|cssoff|csson"/><br/>
<input id='topic' type='text' value='' rel="writehere|type topic|cssoff|csson"/><br/>
</form>

and this is the javascript function that add events and styles when your document is ready:

function setWriteHere(search) {
	/* add on click event handler */
	$("input[rel^="+search+"]").click( function () {
		var ar = $(this).attr('rel').split('|');
		if($(this).val()==ar[1]) { $(this).val(""); $(this).attr('class',ar[3]); }
	} );
	/* add on blur event handler */
	$("input[rel^="+search+"]").blur( function () {
		var ar = $(this).attr('rel').split('|');
		if($(this).val()=="") { $(this).val(ar[1]); $(this).attr('class',ar[2]); }
	} );
	/* trigger blur event */
	$("input[rel^="+search+"]").each( function () { $(this).blur(); } );

}

$(document).ready(function() {
	/* fix forms when ready */
	setWriteHere("writehere");
	}
);
Share

Tags: , , ,