jQuery Impromptu

About

jQuery Impromptu is an extension to help provide a more pleasant way to spontaneously prompt a user for input. More or less this is a great replacement for an alert, prompt, and confirm. Not only does it replace these but it also allows for creating forms within these controls. This is not intended to be a modal replacement, just a quick tool to prompt user input in a fashionable way.

Car BounceTry my new app to keep you informed of your car's financing status and value.

Donation

Has Impromptu been helpful to you?

Download

Version

Version 4.0.1

Last updated on 03/03/2012

jQuery Impromptu is currently available for use in all personal or commercial projects under both MIT and GPL licenses. This means that you can choose the license that best suits your project, and use it accordingly.

GPL License

MIT License

JQuery Impromptu is minified using Gedit Clientside Plugin.

Docs

There are many options with Impromptu:

$.prompt( msg , options )

msg

The message can either be a string, or an object of "states". Each state has the following properties:

html
A string of html or text for the content
buttons
An object containing the text and values of each button the user may click. Default: { Ok : true }
focus
Index of the button to focus(0,1,2..). Default: 0
submit
A function to be called when the prompt is submitted. Default: function(event, value, message, formVals){} Return false or event.preventDefault() to keep the prompt open. Event object also has properties event.state and event.stateName.
var temp = {
	state0: {
		html:'test 1...',
		buttons: { Cancel: false, Next: true },
		focus: 1,
		submit:function(e,v,m,f){ }
	},
	state1: {
		html:'test 2..',
		buttons: { Back: -1, Exit: 0, Next: 1 },
		focus: 2,
		submit:function(e,v,m,f){ }
	}
};

If a string is passed as the message in place of a state, buttons, focus, and submit will be substituted from the options below (just as with earlier versions of Impromptu).

options

loaded
A function to be called when the prompt is fully loaded Default: function(event){}
submit
See state submit event above.
statechanging
A function to be called when a state is about to change. Default: function(event, fromStatename, toStateName){} Return false or event.preventDefault() to prevent the state change.
statechanged
A function to be called when a state has changed. Default: function(event, toStateName){}
callback
A function to be called when the prompt is submitted and removed. Default: function(event[, value, message, formVals]){} Last three paremeters available only when button was clicked.
buttons
An object containing the text and values of each button the user may click. Default: { Ok : true }
prefix
A prefix to be used for all css classes and html object id's. Default: 'jqi'
classes
Add class names to the outer Impromptu shell (parent of the fade and prompt) Default: ''
focus
Index of the button to focus(0,1,2..). Default: 0
zIndex
zIndex to apply to the prompt. Default: 999
useiframe
Will use an iframe for the overlay in IE6 to cover up <select>. Default: false
top
Distance from the top of the screen the prompt will be Default: 15%
opacity
The prefered opacity of the transparent layer placed over the container. Default: 0.6
overlayspeed
The prefered speed of the fadeIn and fadeOut of the overlay ("slow", "fast", number) Default: "slow"
promptspeed
The prefered opacity of the showing of the prompt ("slow", "fast", number). Default: "fast"
show
Name of the jQuery method to animate the entrance of the prompt("show","fadeIn","slideDown"). Default: "promptDropIn"
persistent
If the prompt should close when the fade is clicked (true doesn't close). Default: true
timeout
The number of milliseconds until the prompt automatically closes. Default: 0

returns

returns a jQuery object of the prompt box and fade. Note: This has changed to a container with everything within it, fade and prompt box.

Events using Bind

promptloaded
Same as loaded option above. Scope is the entire prompt and fade container.
promptsubmit
Same as loaded option above. Scope is the current state.
promptclose
Same as callback option above. Scope is the entire prompt and fade container.
promptstatechanging
Same as statechanging option above. Scope is the entire prompt and fade container.
promptstatechanged
Same as statechanged option above. Scope is the entire prompt and fade container.

Example of binding an event.

var myPrompt = jQuery.prompt(/*...*/);
myPrompt.bind('promptloaded', function(e){});

Helper Functions

jQuery.prompt.setDefaults(options)
Sets the defaults for prompts.
jQuery.prompt.setDefaults({
	prefix: 'myPrompt',
	show: 'slideDown'
});
jQuery.prompt.setStateDefaults(options)
Sets the defaults for states.
jQuery.prompt.setStateDefaults({
	buttons: { Ok:true, Cancel:false },
	focus: 1
});
jQuery.prompt.getCurrentState()
Returns a jquery object of the current visible state.
jQuery.prompt.getCurrentStateName()
Returns a string current visible state's name.
jQuery.prompt.getStateContent(stateName)
Returns a jquery object of the state, so you can update the content within it.
jQuery.prompt.goToState(stateName, callback)
Transitions to the specified state. Callback represents a statechanged event.
jQuery.prompt.nextState(callback)
Transitions to the next state. Callback represents a statechanged event.
jQuery.prompt.prevState(callback)
Transitions to the previous state. Callback represents a statechanged event.
jQuery.prompt.close()
Closes the prompt.

Demonstrations

Example CSS

Below is an example of the css format that could be used with Impromptu. You dont need to use any positioning and transparency attributes as Impromptu takes care of those for you.

Each element within your css should resemble the prefix option you supply to the function call. If a prefix isn't supplied jqi is used. The CSS for all examples can be found in examples.css

.jqifade{
	position: absolute;
	background-color: #aaaaaa;
}
div.jqi{
	width: 400px;
	font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
	position: absolute;
	background-color: #ffffff;
	font-size: 11px;
	text-align: left;
	border: solid 1px #eeeeee;
	border-radius: 10px;
	-moz-border-radius: 10px;
	-webkit-border-radius: 10px;
	padding: 7px;
}
div.jqi .jqicontainer{
	font-weight: bold;
}
div.jqi .jqiclose{
	position: absolute;
	top: 4px; right: -2px;
	width: 18px;
	cursor: default;
	color: #bbbbbb;
	font-weight: bold;
}
div.jqi .jqimessage{
	padding: 10px;
	line-height: 20px;
	color: #444444;
}
div.jqi .jqibuttons{
	text-align: right;
	padding: 5px 0 5px 0;
	border: solid 1px #eeeeee;
	background-color: #f4f4f4;
}
div.jqi button{
	padding: 3px 10px;
	margin: 0 10px;
	background-color: #2F6073;
	border: solid 1px #f4f4f4;
	color: #ffffff;
	font-weight: bold;
	font-size: 12px;
}
div.jqi button:hover{
	background-color: #728A8C;
}
div.jqi button.jqidefaultbutton{
	background-color: #BF5E26;
}
.jqiwarning .jqi .jqibuttons{
	background-color: #BF5E26;
}

.jqi .jqiarrow{ position: absolute; height: 0; width:0; line-height: 0; font-size: 0; border: solid 10px transparent;}

.jqi .jqiarrowtl{ left: 10px; top: -20px; border-bottom-color: #ffffff; }
.jqi .jqiarrowtc{ left: 50%; top: -20px; border-bottom-color: #ffffff; margin-left: -10px; }
.jqi .jqiarrowtr{ right: 10px; top: -20px; border-bottom-color: #ffffff; }

.jqi .jqiarrowbl{ left: 10px; bottom: -20px; border-top-color: #ffffff; }
.jqi .jqiarrowbc{ left: 50%; bottom: -20px; border-top-color: #ffffff; margin-left: -10px; }
.jqi .jqiarrowbr{ right: 10px; bottom: -20px; border-top-color: #ffffff; }

.jqi .jqiarrowlt{ left: -20px; top: 10px; border-right-color: #ffffff; }
.jqi .jqiarrowlm{ left: -20px; top: 50%; border-right-color: #ffffff; margin-top: -10px; }
.jqi .jqiarrowlb{ left: -20px; bottom: 10px; border-right-color: #ffffff; }

.jqi .jqiarrowrt{ right: -20px; top: 10px; border-left-color: #ffffff; }
.jqi .jqiarrowrm{ right: -20px; top: 50%; border-left-color: #ffffff; margin-top: -10px; }
.jqi .jqiarrowrb{ right: -20px; bottom: 10px; border-left-color: #ffffff; }

Moopromptu - A few Differences

The Mootools version of Impromptu enjoys all the same features with only a couple exceptions. First "Moopromptu" uses $prompt to closer adhear to Mootools standards. Secondly to open a prompt you must call show(msg, options) instead of the base $.prompt(msg, options);

$prompt.show(msg, options);

If you want to make your own instance of Moopromptu instead of using $prompt, you can do so like this:

var myPrompt = new Impromptu();
                myPrompt.show('hello world');

Examples

To simply call Impromptu like you would a regular alert command:

$.prompt('Example 1');

To add a a couple extra buttons with different values:

$.prompt('Example 2',{ buttons: { Ok: true, Cancel: false } });

To change the opacity of the fading overlay:

$.prompt('Example 3',{ opacity: 0.2 });

To change the default focused button:

$.prompt('Example 4',{ buttons: { Ok: true, Cancel: false }, focus: 1 });

To change the css name use prefix:

$.prompt('Example 5',{ prefix: 'impromptu' });

To change the prompt show effect:

$.prompt('Example 6',{ show:'slideDown' });

To use your own jQuery effect:

$.fn.extend({
	dropIn: function(speed, callback){
		var $t = $(this);

		if($t.css("display") == "none"){
			eltop = $t.css('top');
			elouterHeight = $t.outerHeight(true);

			$t.css({ top: -elouterHeight, display: 'block' })
				.animate({ top: eltop },speed,'swing', callback);
		}
	}
});
	
$.prompt('Example 7',{ show:'dropIn' });

To add a callback function:

function mycallbackfunc(e,v,m,f){
	alert('i clicked ' + v);
}

$.prompt('Example 8',{ callback: mycallbackfunc });

The callback function has three parameters.

If a form were in the prompt you could access its elements within this second parameter. The values of the form fields are gathered for you within the third parameter.

var txt = 'Please enter your name:<br />
	<input type="text" id="alertName" 
	name="alertName" value="name here" />';
	
function mycallbackform(e,v,m,f){
	if(v != undefined)
		alert(v +' ' + f.alertName);
}

$.prompt(txt,{
	callback: mycallbackform,
	buttons: { Hey: 'Hello', Bye: 'Good Bye' }
});

The submit function can prevent the box from closing and the callback function from being called by returning false. This is great for validating input before closing the prompt. You must always return true or false with this function.

var txt = 'Try submitting an empty field:<br /> 
	input type="text" id="alertName" 
	name="myname" value="" />';
function mysubmitfunc(e,v,m,f){
	an = m.children('#alertName');

	if(f.alertName == ""){
		an.css("border","solid #ff0000 1px");
		return false;
	}
	return true;

}

$.prompt(txt,{
	submit: mysubmitfunc,
	buttons: { Ok:true }
});

Impromptu plays very nicely with other jQuery extensions like the jQuery Corner Plugin

$.prompt('Example 11',{prefix:'impromptu'}).children('#impromptu').corner();

Give it a complete make over! Some buttons, a different prefix, a new entrance effect and some rounded corners! The css is in the CSS file for this page and the slideDown effect comes in the jQuery library.

$.prompt('Example 12<p>Save these settings?</p>',{ 
	buttons:{ Apply:1,Maybe:2,Never:3 },
	prefix:'colsJqi',
}).children('#colsJqi').corner();

A nice light brown theme.

var brown_theme_text = '<h3>Example 13</h3>'+
	'<p>Save these settings?</p>'+

	'<img src="images/help.gif" alt="help" '+
	'class="helpImg" />';
$.prompt(brown_theme_text,{
	buttons:{Ok:true,Cancel:false}, 
	prefix:'brownJqi'
});

A nice clean blue theme.

$.prompt('Hello World!!',{
	buttons:{Ok:true,Cancel:false},
	prefix:'cleanblue'
});

A nice clean blue theme with an Ext feel.

$.prompt('Hello World!!',{
	buttons:{Ok:true,Cancel:false}, 
	prefix:'extblue'
});

A smooth theme based on the default theme.

$.prompt('Hello World!!',{ buttons:{Ok:true,Cancel:false}, prefix:'jqismooth' });

A simple States example. For a more detailed example please see this States Demo

var statesdemo = {
		state0: {
			html:'test 1.<br />test 1..<br />test 1...',
			buttons: { Cancel: false, Next: true },
			focus: 1,
			submit:function(e,v,m,f){ 
				if(!v) return true;
				else
					$.prompt.goToState('state1');
				return false; 
			}
		},
		state1: {
			html:'test 2',
			buttons: { Back: -1, Exit: 0 },
			focus: 1,
			submit:function(e,v,m,f){ 
				if(v==0) $.prompt.close()
				else if(v=-1)
				$.prompt.goToState('state0');
				return false;
			}
		}
};

$.prompt(statesdemo);

Pass key/value pairs as buttons for more complex button titles.

$.prompt('Hello World!!',{
	buttons:[
		{title: 'Hello World',value:true},
		{title: 'Good Bye',value:false}
		], 
	submit: function(e,v,m,f){ alert(v); } 
});

To create a tour you first start with states, then give each state some positioning instructions. We'll create a quick method as well to help navigate.

The positioning instructions are provided through an option WITHIN each state. position: { container: '#header', x: 10, y: 45, width: 200, arrow: 'tl' }

The container is the anchor element selector, and x/y are the distances relative to the anchor element. The arrow options are: tl, tc, tr, rt, tm, tb, br, bc, bl, lb, lm, lt.

var tourSubmitFunc = function(e,v,m,f){
			if(v === -1){
				$.prompt.prevState();
				return false;
			}
			else if(v === 1){
				$.prompt.nextState();
				return false;
			}
},
tourStates = [
	{
		html: 'Welcome to jQuery Impromptu, lets take a quick tour of the plugin.',
		buttons: { Next: 1 },
		focus: 1,
		position: { container: '#header', x: 10, y: 45, width: 200, arrow: 'tl' },
		submit: tourSubmitFunc
	},
	{
		html: 'When you get ready to use Impromptu, you can get it here.',
		buttons: { Prev: -1, Next: 1 },
		focus: 1,
		position: { container: '#downloadHeader', x: 170, y: -10, width: 300, arrow: 'lt' },
		submit: tourSubmitFunc
	},
	{
		html: "You will also need this CSS",
		buttons: { Prev: -1, Next: 1 },
		focus: 1,
		position: { container: '#cssHeader', x: 40, y: -100, width: 250, arrow: 'bl' },
		submit: tourSubmitFunc
	},
	{
		html: 'A description of the options are under the Docs section.',
		buttons: { Prev: -1, Next: 1 },
		focus: 1,
		position: { container: '#docsHeader', x: 115, y: -85, width: 200, arrow: 'lb' },
		submit: tourSubmitFunc
	},
	{
		html: 'You will find plenty of examples to get you going.. including this tour..',
		buttons: { Prev: -1, Next: 1 },
		focus: 1,
		position: { container: '#examplesHeader', x: -300, y: -45, width: 250, arrow: 'rm' },
		submit: tourSubmitFunc
	},
	{
		html: 'Yep, see, creating a tour is easy.. Here is the source:',
		buttons: { Prev: -1, Next: 1 },
		focus: 1,
		position: { container: '#tourExample', x: -340, y: 5, width: 300, arrow: 'rt' },
		submit: tourSubmitFunc
	},
	{
		html: 'This concludes our tour. If you found Impromptu helpful, please see the links to the left, if not, thanks for stopping by!',
		buttons: { Done: 2 },
		focus: 1,
		position: { container: '#donationHeader', x: 420, y: 0, width: 300, arrow: 'lm' },
		submit: tourSubmitFunc
	}
];
$.prompt(tourStates, { opacity: 0.3 });