Easy Input CSS Rules

I ran across a post on the DOM Scripting blog, the other day, and saw a great opportunity to demo the brevity of jQuery, observe:

Old DOM Way:

function appendInputTypeClasses() {
  if ( !document.getElementsByTagName ) return;
  var inputs = document.getElementsByTagName('input');
  var inputLen = inputs.length;
  for ( i=0; i < inputLen; i++ ) {
    if ( inputs[i].getAttribute('type') ) {
      inputs[i].className += ' '+
        inputs[i].getAttribute('type');
    }
  }
}

New jQuery Way:

$("input").each(function(){
   $(this).addClass(this.type);
});

I’m even considering implementing a new way of circumventing the each() function, observe:

$("input").addClass("{type}");

You really can’t get much shorter than that, when it comes to Javascript code. I have quite a few more “old” DOM scripting examples that I’ll be posting/improving in the next couple days.

Posted: January 28th, 2006


Subscribe for email updates

Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

John Resig Twitter Updates

@jeresig / Mastodon

Infrequent, short, updates and links.