Alessandro Melandri

WebSphere Commerce Specialist, project manager, wannabe photographer

Javascript getElementById() Shortcut

Do you love the wonderful javascript dollar shortcut $('myId') but cannot use jQuery or Prototype? Don’t worry, you can define your custom shortcut for the too long document.getElementById() function:

1
2
3
function $(id) {
  return document.getElementById(id);
}

Now you can sobstitute this:

1
var divContent = document.getElementById('myId').innerHTML;

with this:

1
var divContent = $('myId').innerHTML;

Obviously you don’t get all the bells and whistles that you would get using jQuery, but it’s a start.