Corey Coogan

Python, .Net, C#, ASP.NET MVC, Architecture and Design

  • Subscribe

  • Archives

  • Blog Stats

    • 110,580 hits
  • Meta

Archive for the ‘UI’ Category

Using jQuery to Dynamically Display a Slideout

Posted by coreycoogan on May 19, 2011

Here’s something I find myself needing to do often enough that it warrants a post. Here’s the situation…

I am dynamically displaying an unknown number of data items. The item itself is unimportant for this post, however, the item may contain some attributes that shouldn’t be displayed on the page all the time.  Perhaps it’s a  long description or information about how the item was last updated.  Whatever it is, this data is better to be hidden unless someone actually wants to see it.  This may be a good use case for some sort of panel that slides out when an “info” icon or “>>” button is clicked.

Making this happen with jQuery is pretty simple and consists of a few steps:

  1. Wire up a “click” event to the button or text from where the panel will slide.
  2. Using the clicked on element as an anchor, use jQuery’s offset() method to get the coordinates.
  3. Either clone an existing element, such as a DIV, or create one on the fly and assign the following CSS rules to it:
    1. Position – This should be set to absolute so the panel displays relative to its containing element.
    2. Top – The distance in pixels, from the top of the containing element, where the panel should be placed.
    3. Left – The distance in pixels, from the left of the containing element, where the panel should be placed.
  4. Append the new panel to the container that is holding the list of data items.
  5. Display the panel using a cool effect – or hide it.
 A couple of notes before we get to the example.  First, there are 2 ways to get an element’s position in jQuery – offset() and position().  You can read the descriptions to understand what they both do, but offset() typically works for me. I’m also using the ‘slide’ toggle effect from jQuery UI to slide my panel from left to right with the toggle() method.

Now for an example:

<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript">

	//either get data from a service here or have it ready from server-side code


function showMoreInfo(anchor,itemId)
	//make a jQuery object
	anchor = $(anchor);

	//the dynamic ID of the moreInfo panel
	var panelId = 'moreInfo_' + itemId;
	//see if the panel already exists
	var info = anchor.parent().parent().find('#' + panelId);

		//the panel doesn't exist, so create it

		//get the coordinates from our anchor
		var coords = anchor.offset();
		//clone our info panel, or use jQuery templates
		info = $('#moreInfoPanel').clone();

		info.find('#description').html('<b>Get this data based on the itemId</b>');
		info.find('#lastUpdated').html('<b>Get this data based on the itemId</b>');

		//set the new dynamic ID

		//now set the CSS to make the panel display where we want
			position: 'absolute',
			left: coords.left+anchor.width() + 15
		//append the panel to the item div

	//use the jquery UI slide definition
		//slide callback to change the button text depending on the state
			anchor.html('<< Less'); 		}else{ 			anchor.html('More >>');



<div id='moreInfoPanel' style='display:none;background:silver;padding:20px;'>
	<div id='description'></div>
	<div id='lastUpdated'></div>

<div id='dataItems'>

	<!-- this is an example data item. this would be generated dynamically server side or from a web service -->
	<!-- jQuery Templates could be nice here -->
	<div id='dataItem_ITEMID' class='dataItem' style='border:1px solid black;width:200px'>
		<div style='text-align:right'>
			<!-- wire up the click event -->
			<button onclick='javascript:showMoreInfo(this,"ITEMID");'>More >></button>
			Main content goes here



Posted in jQuery, UI | Tagged: , , , , , | Comments Off on Using jQuery to Dynamically Display a Slideout

JQuery’s Dialog and Form Problems

Posted by coreycoogan on December 1, 2010

Sometimes there is a need to put certain elements within a FORM in a JQuery Dialog.  This sounds simple enough, but unfortunately when the dialog is displayed, the form elements can get lost as they get added outside the Form’s DOM.

The solution is simple, append the dialog element to the form. Here’s an example of how it’s done.

$(document).ready(function() {

       //define the dialog for later use
        var dlg = $('#AddressVerification').dialog(
            autoOpen: false,
            closeOnEscape: false,
            modal: true,
            width: 550 

        //This is where we tie the dialog content to the parent form
		//other code not pertinent to this
<!-- form elements -->

<div id="AddressVerification" style="display: none;">
        <!-- address form elements -->
            <button id="btnSave">Save</button>
            <button id="btnCancel">Cancel</button>


Posted in jQuery, UI | 2 Comments »

Adding Icons to ValidationSummary and Information Boxes with JQuery

Posted by coreycoogan on November 2, 2009

What’s so special about ValidationSumary and Information Boxes?

When there are errors or important information that you need to call out to your users, it is important to make it unmistakably noticeable. You want to stack the odds in your favor as much as possible that the user will see your call outs and act on them accordingly. Unfortunately, the ValidationSummary in ASP.NET and ASP.NET MVC comes very vanilla. You can change the appearance in MVC by defining the “validation-summary-errors” CSS class that is set to the ValidationSummary by default, but this isn’t always enough. The same goes true for an Information Box, which is what I call a box that contains important information that I want the user to see.

It is very typical for users to gloss over these important messages when they are rendered as text only. Placing the text in a colored box with a brightly colored border can help, but I’ve still experienced frustrated users claiming that registration is broken because they miss the message that says their email has already been used. To combat this, it’s a good practice to use familiar operating system icons for Error, Information, Warning, etc. Most users will recognize familiar icons and have learned to pay attention when they see them.

Here’s an example of what we’re going to achieve:

Error callout box

Info Callout Box

Styling the “Callout Box”

I use the same technique to style all my callout boxes. It’s very simple to do with JQuery, CSS and a few icons. I’ll demonstrate this for both an Error and Info callout.

First, the CSS classes. Each type of callout (error, info, warning) will require 2 classes. One class will style the box that contains the text and the other will be used to hold the applicable icon.

    border: solid 1px #228ef1; 
    padding: 5px 8px 5px 8px;

   padding: 5px 15px 5px 15px;
    background:#F1E7D1 url(/images/icons/info-24x24.png) no-repeat;
    background-position:left center;

    border: solid 1px red; 
    padding: 5px 8px 5px 8px;

   padding: 5px 15px 5px 15px;
    background:#F1E7D1 url(/images/icons/err-24x24.png) no-repeat;
    background-position:left center;

For my error callouts, I want to add one more class. This will handle the heading of my errors, which typically reads something like “Please fix the following errors”.


Using JQuery to Construct the Callout Box

The HTML used to put the icon in the callout box requires nested DIV or SPAN elements. I don’t want to depend on my memory to always add the nested elements each time I want a callout, I want to simply write something like this:

<div id="CustomSearchSelect" class="infobox">
            <strong>Can't Find Your School?</strong> Try a <a id="CustomSearchLink" href="#">Custom Search</a>.

<div class='errorbox'>
This action is not allowed!

That’s where JQuery is able to help us. By using the prepend function, we can have the appropriate icon automatically injected into our callout box. We do this by adding the following to our JQuery ready function in our Master Page.

$(document).ready(function() {
$(".infobox").prepend("<span class='infoimage'>&nbsp;</span>");
$(".errorbox").prepend("<span class='errorimage'><span class='errorhead'>Looks like we have a small problem...</span></span>");

Pretty simple, right? Here’s what’s happening here. The script is finding the elements styled with the infobox and errorbox class and sticking in a SPAN element styled with the corresponding icon class. The errorbox gets the additional heading added here as well. You could do the same thing for Warn, Question or any other conventions you wish to follow.

Styling the ValidationSummary

The ValidationSummary requires a little bit of extra effort. This is because I’m using the xVal 1.0 and a ValidationSummary for client-side error handling as described in my last blog post. The only thing I need to do is define the HTML for my ValidationSummary, but hide it unless I have Model Errors in my MVC application. This is done by wrapping my ValidationSummary in a div styled with the errorbox class like so.

<% string display = ViewData.ModelState.IsValid ? "none" : "block"; %>
<div class="errorbox" id="validationSummary" style="display:<%=display%>"> 
    <%= Html.ValidationSummary() %>

Finding the Icons

I found some pretty nice icons on various free icon websites. It took a bit of time to run them all down, so I’m including them here for you in a zip file.

Icon Zip File

Posted in ASP.NET, ASP.NET MVC, CSS, jQuery, UI | Tagged: , , , , | 4 Comments »