Mark Sanborn Mark Sanborn Fishing Mark Sanborn

Coder, Entrepreneur, Blogger, and Coffee Addict

Highlight Form Focus with Prototype

Posted on by Mark Sanborn

I recently made a purchase through Paypal (with my trusty paypal security key) and noticed that their forms really stood out. They had an effect on their forms that would highlight the row in which you are currently typing. I thought this was a very elegant use of ajax that really added value to the form. This is why I created my own version of it.

If you are like me you will want to see the effect before you read on.

You can also go to Paypal and see their version of the effect.

The html

 

<html>
<head>
<link rel="stylesheet" type="text/css" href="screen.css" />
<script src="js/prototype.js" type="text/javascript"></script>
<script src="js/scriptaculous.js" type="text/javascript"></script>
</head>
<body>
<form action="" method="post">
<table>
<tr id="tr1"><td>Text Field 1: </td><td><input name="input 1" type="text"/></td></tr>
<tr id="tr2"><td>Text Field 2: </td><td><input name="input 2" type="text"/></td></tr>
<tr id="tr3"><td>Text Field 3: </td><td><input name="input 3" type="text"/></td></tr>
<tr id="tr4"><td>Text Field 4: </td><td><input name="input 4" type="text"/></td></tr>
<tr id="tr5"><td>Text Field 5: </td><td><input name="input 5" type="text"/></td></tr>
<tr id="tr6"><td>Text Field 6: </td><td><input name="input 6" type="text"/></td></tr>
<tr id="tr7"><td>Drop down: </td><td><select name="checkBox">
			<option>yes</option>
			<option>no</option>
		</select></td></tr>
<tr id="tr8"><td>Drop down 2: </td><td><select name="checkBox">
			<option>yes</option>
			<option>no</option>
		</select></td></tr>
<tr id="tr9"><td>Check Box: </td><td><input name="checkBox" type="checkbox" value="checkBox"/></td>

<tr><td></td><td><input type="submit" value="submit" /></td></tr>
</form>
</table>
</body>
</html>

The Ajax

 
Now we add the ajax above the form. This will add the css class, “highlight” to each table row. And also remove the class when called upon.

<script language='javascript'>
function highlightRow(theRow) {
	$(theRow).addClassName('highlight');
}
function unhighlightRow(theRow) {
	$(theRow).removeClassName('highlight');
}
</script>

onFocus and onBlur

 
Now all there is to do is add the onFocus and onBlur to the input tags in our form. Add this with the corresponding table row.

onFocus="highlightRow('tr1')" onBlur="unhighlightRow('tr1')"

Thats it…

Don’t for get to See a live working example.



This entry was posted in Programming. Bookmark the permalink.

2 Responses to Highlight Form Focus with Prototype

  1. Qup says:

    AJAX = Asynchronous Javascript And XML.

    Your script does not contain any Ajax. Just javascript. It does use a few prototype functions, which is a library that also supports Ajax, but that’s not quite the same as using ajax itself.

  2. Mark Sanborn says:

    I admit, I misused the AJAX buzzword. :P

© 2011 All rights reserved

1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139|140|141|142|143|144|145|146|147|148|149|150|151|152|153|154|155|156|157|158|159|160|161|162|163|164|165|166|167|168|169|170|171|172|173|174|175|176|177|178|179|180|181|182|183|184|185|186|187|188|189|190|191|192|193|194|195|196|197|198|199|200|201|202|203|204|205|206|207|208|209|210|211|212|213|214|215|216| generic buying erythromycin alesse next day delivery without a prescription motilium no prescription needed buy cytotec pills canadian pharmacy no prescription needed provera actos by internet buying orlistat pharmacy without prescription lisinopril no prescription needed purchase femara medication buy lipitor cod actonel buy online cheap buy carbozyne online without prescription femara prescription discounts elavil without prescriptions pills buying isotretinoin online pharmacy trazodone no prescription order no prescription metformin birth control no prescriptionAccutane Online Doxycycline online Buy Cheap Lexapro Online No Prescription Prednisone Online Buy Accutane No Prescription