Replaceholder jQuery Plugin

In two recent posts I used a simple JavaScript function to replace the placeholder attribute of an input element with a value attribute.  Since I’ve used it twice now I decided to make it into a simple jQuery plugin and wanted to give it to everyone else as well. The best part is you can now start using this HTML5 attribute before it’s fully supported!

Demo | Download

What does it do?

Replaceholder simply scans your file for input elements with the placeholder attribute and replaces it with a value attribute for cross-browser feng shui. It mimics the basic placeholder functionality by saving the placeholder value of each field and on focus removes the value from the input. As the user interacts with the field it removes and replaces the value of the field unless the user has added content. From this:

<input type="text" class="search" placeholder="Search" />

To this:

<input type="text" class="search" value="Search" />

How it works

  1. Download the plugin
  2. Unzip the file and grab either the development version (jquery.replaceholder.js) or the production version (jquery.replaceholder-min.js)
  3. Add the <script> tag
    <script type="text/javascript" src="path/to/file/jquery.replaceholder.min.js"></script>
  4. Run plugin
    $.fn.replaceholder();
  5. ???
  6. Profit!

That’s all you need to do.

Copypasta?

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="path/to/file/jquery.replaceholder.min.js"></script>
<script type="text/javascript">
$(function() {
	$.fn.replaceholder();
});
</script>

Using the above code you should only have to update “path/to/file/” with the location to your saved file!

Customizing the Plugin

You can change the focus text color and the blur text color by passing these two properties:

$.fn.replaceholder({
	focusColor:'#000',
	blurColor:'#aaa'
});

You can also specify the elements you want to apply replaceholder to:

$('.search').replaceholder();

Feel free to use and abuse this code. If you end up using it let me know! Enjoy!

Prototype in CSS

Over the last three years I have slowly been learning JavaScript and one of my favorite and still somewhat most confusing ideas go grasp is JavaScript’s use of Prototyping with Objects. I get the basics that a prototype is a copy of the original object with all it’s properties, but I haven’t been able to use it much in my day to day.

As I’ve experimented with CSS3 the one thing I’ve really struggled with is the crazy amount of extra code you end up writing in order to achieve cross-browser chi. Then it hit me, what if prototyping was extended to CSS. The idea isn’t that complex, but it’s all in my head at this point. Here is the basic idea:

I first start by defining a class that has a drop shadow property, and it’s set for all browsers, then reference that class using a prototype attribute:

.shadow-class {
	-moz-box-shadow:0 1px #000;
	-webkit-box-shadow:0 1px #000;
	box-shadow:0 1px #000;
}

#my_div { prototype:".shadow-class"; }

The basic idea is to add the attribute “prototype” and it accepts an existing Class or ID and then adds those styles to the element.  This is along the lines of variables in CSS, but it’s a little more complex. If any browser vendors would like to play around with the idea I would love to see it.

What do you guys think? Do you think it would be worth while and does it make sense to you?

Apple Navigation with CSS3

Apple is known for very clean design and if you have been to their website in the last few years you’ve seen their primary navigation. It’s a staple of the apple website and today I wanted to take a crack at recreating the Apple navigation using CSS3 techniques.

The Markup

<ul id="nav">
	<li><a href="#" class="apple"><span>Apple</span></a></li>
	<li><a href="#">Store</a></li>
	<li><a href="#">Mac</a></li>
	<li><a href="#">iPod</a></li>
	<li><a href="#">iPhone</a></li>
	<li><a href="#">iPad</a></li>
	<li><a href="#">iTunes</a></li>
	<li><a href="#">Support</a></li>
	<li class="search_container">
		<form class="search" method="get" action="#">
			<label for="search">
				<input type="text" id="search" placeholder="Search" />
			</label>
		</form>
	</li>
</ul>

I wanted to keep it very light weight and semantic. Using an unordered list is fairly standard for a navigation system and I’m not really breaking new ground here.

I built this with an additional class named “current” which will set when you are viewing a specific page.

The CSS

#nav {
	background:-webkit-gradient(linear, 0 0, 0 100%, from(#cacaca), to(#848484));
	background:-moz-linear-gradient(top, #cacaca, #848484);
	-webkit-border-radius:3px;
	-moz-border-radius:3px;
	border-radius:3px;
	-webkit-box-shadow:0 1px 2px rgba(0,0,0,.3);
	display:inline-block;
	list-style:none;
	margin:0 0 20px;
	overflow:hidden;
	padding:0;
}
#nav li { border-right:1px solid #808080; -webkit-box-shadow:inset 0 0 0 1px rgba(255,255,255,.1); -moz-box-shadow:inset 0 0 0 1px rgba(255,255,255,.1); float:left; }
#nav li a { color:#262626; display:block; font:13px "Lucida Sans Unicode", "Lucida Grande", sans-serif; height:36px; line-height:34px; padding:0 30px; text-decoration:none; text-shadow:0 1px #cecece; }
#nav li a:hover { background:-webkit-gradient(linear, 0 0, 0 100%, from(#929292), to(#535353)); background:-moz-linear-gradient(top, #929292, #535353); -webkit-box-shadow:inset 0 -10px 20px rgba(0,0,0,.05), inset 0 0 0 1px rgba(255,255,255,.1); -moz-box-shadow:inset 0 -10px 20px rgba(0,0,0,.05), inset 0 0 0 1px rgba(255,255,255,.1); color:#fff; text-shadow:0 -1px #414141; }
#nav li a:active { -webkit-box-shadow:inset 0 -10px 20px rgba(0,0,0,.05), inset 0 2px 5px #363636, inset 0 0 0 1px rgba(255,255,255,.1); -moz-box-shadow:inset 0 -10px 20px rgba(0,0,0,.05), inset 0 2px 5px #363636, inset 0 0 0 1px rgba(255,255,255,.1); }
#nav li .apple { padding:0; }
#nav li .apple:hover { -moz-border-radius-topleft:3px; -moz-border-radius-bottomleft:3px; -webkit-border-top-left-radius:3px; -webkit-border-bottom-left-radius:3px; }
#nav li .apple span { background:url(images/apple.png) center 6px no-repeat; display:block; padding:0; text-indent:-99999em; width:102px; }
#nav li .current,
#nav li .current:hover { background:-webkit-gradient(linear, 0 0, 0 100%, from(#373737), to(#525051)); background:-moz-linear-gradient(top, #373737, #525051); -webkit-box-shadow:inset 0 -10px 20px rgba(0,0,0,.05), inset 0 2px 5px #363636, inset 0 0 0 1px rgba(255,255,255,.1); -moz-box-shadow:inset 0 -10px 20px rgba(0,0,0,.05), inset 0 2px 5px #363636, inset 0 0 0 1px rgba(255,255,255,.1); color:#fff; text-shadow:0 1px #242323; }
#nav .search_container { border-right:none; padding-right:5px; }
#nav .search { background:url(images/site-search-sprite.png) right -53px no-repeat; padding:7px 20px 8px 5px; }
#nav .search label { background:url(images/site-search-sprite.png) 0 0 no-repeat; display:block; height:20px; }
#nav .search input { border:none; color:#a9a9a9; height:12px; margin-left:25px; -webkit-appearance:none; }
#nav .search input:focus { border:none; outline:none; }

Things to note in the CSS would be multiple box-shadow properties.

-webkit-box-shadow:inset 0 -10px 20px rgba(0,0,0,.05), inset 0 2px 5px #363636, inset 0 0 0 1px rgba(255,255,255,.1);

The above code is used for the active state. This is triggered when you click one of the buttons. You can see I have chained three different styles to one property using a comma separated list. I tend to use box-shadow a lot to add a lot of detail to the end result. The great thing about using CSS rather than slicing images is the amount of time I saved.  I was able to put this example together in a few hours.  In fact it has taken me longer to write about what I’ve done that it actually took to create it. I think that says more about me as a writer than it says about my programming skills, but that’s not the point! :)

Making it work on Mobile Safari was something I wanted to get right this time. I did some low level Googling and found the -webkit-appearance property.

-webkit-appearance:none;

By setting it to ‘none’ I am able to remove the rounded corners and inner shadow from the element.

The JavaScript

I was able to use some code from my last posting and tweak it a little bit to give it some extra functionality.

$(function() {

	var $placeholder = $('input[placeholder]');

	if ($placeholder.length > 0) {

		var attrPh = $placeholder.attr('placeholder');

		$placeholder.attr('value', attrPh)
		  .bind('focus', function() {

			var $this = $(this),
				$form = $this.parents('.search');

			if($this.val() === attrPh)
				$this.val('').css('color','#333');

			$form.addClass('focus');

		}).bind('blur', function() {

			var $this = $(this),
				$form = $this.parents('.search');

			if($this.val() === '')
				$this.val(attrPh).css('color','#a9a9a9');

			$form.removeClass('focus');
		});
	}
});

What does this script do? Well to start I define my variables, which is a great practice to get into. I use $ in front of my variables to let me know I’m dealing with a jQuery object. I think check to see if any of those objects exist on the page. From there it’s pretty simple.  I grab the placeholder value and duplicate it as a value attribute.  On focus, or when you click into the text field, I’m checking to see if the text in the field matches the text that was in the placeholder attribute.  If they are the same then I remove the text. Then I find the parent form and set it’s class to “focus” which toggles the style of the blue outline around the search box.

It’s important to note that since this is a demo I grabbed the sprite image from Apple’s website for the search box, saving a local copy to not burn their bandwidth. So while you are, as always, free to copy all my code please note that I do not own the image used for the search box.

I wanted to thank everyone for the great feedback I’ve been receiving lately. It’s so much fun writing these examples and I really enjoy see what everyone has to say about them.  If you have anything that you want to see please let me know in the comments. Please feel free to grab this code or the code from the demo and play with it.  Enjoy!