Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Remco van 't Veer http://blog.remvee.net

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

give focus to first input field or textarea on page

Place the following in your application.js file and make sure your layout includes "javascript_include_tag :defaults".
Event.observe(window, 'load', function() {
  var e = $A(document.getElementsByTagName('*')).find(function(e) {
    return (e.tagName.toUpperCase() == 'INPUT' && (e.type == 'text' || e.type == 'password'))
        || e.tagName.toUpperCase() == 'TEXTAREA' || e.tagName.toUpperCase() == 'SELECT';
  });
  if (e) e.focus();
});

zebra a table

<html>
  <head>
    <script>
      function init () {
	var tables = document.getElementsByTagName("table");
	for (var i = 0; i < tables.length; i++) {
	  if (tables[i].className.match(/zebra/)) {
	    zebra(tables[i]);
	  }
	}
      }

      function zebra (table) {
	var current = "oddRow";
	var trs = table.getElementsByTagName("tr");
	for (var i = 0; i < trs.length; i++) {
	  trs[i].className += " " + current;
	  current = current == "evenRow" ? "oddRow" : "evenRow";
	}
      }
    </script>
    <style>
        tr.oddRow { background-color: green; }
        tr.evenRow { background-color: red; }
    </style>
  </head>
  <body onload="init()">
    <table class="zebra">
      <tr> <td>foo</td> <td>foo</td> <td>foo</td> </tr>
      <tr> <td>bar</td> <td>bar</td> <td>bar</td> </tr>
      <tr> <td>foo</td> <td>foo</td> <td>foo</td> </tr>
      <tr> <td>bar</td> <td>bar</td> <td>bar</td> </tr>
      <tr> <td>foo</td> <td>foo</td> <td>foo</td> </tr>
    </table>
  </body>
</html>
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS