This page describes a function of the Prototype Java Script framework.

The Prototype $ function provides a convenient shortcut to reference to DOM elements by their id.

Syntax


  $('mydiv')  // returns a reference to the element with id 'mydiv'

If it gets anything other than a string, it returns its argument, so that multiple calls to it don’t cause errors. Most functions in Prototype and script.aculo.us that have an element parameter make a call to $ internally, so you can just provide a string containing the id of the element you want to refer to.

Examples


<p id="myp" onclick="demoDollar();Effect.Pulsate(this)">Click me!</p>
<script language="JavaScript">
function demoDollar() {
 alert('With string: ' + $('myp').innerHTML);
 var element = $('myp');
 alert('With element object:' + $(element).innerHTML);
}
</script>

The onclick handler demonstrates that you can refer this (which is the same as $('myp') in this case to Effect.Pulsate, which in turn calls $ internally which just returns the element object.

Demo:

Click me!