Add event for each input with a function

Hi,

please I would to know If I have some input text with the same classname,
can I add an event, for example onclick, for each of them without adding onclick=“function();” in every input text in the code?

for example I have:
<input type=“text” class=“inputbox” name=“text1”>
<input type=“text” class=“inputbox” name=“text2”>
<input type=“text” class=“inputbox” name=“text3”>

Without adding this for each input:
<input type=“text” class=“inputbox” name=“text3” onclick=“function();”>
is it possible to set that onclick event with a js function for every input text with that same classname?

many thanks.

Using jQuery it’s extremely easy:


$("input.class_name").bind("click", function() { 
   // ...function body...
});

If you’ll have input elements added dynamically, you’ll want to use live() or delegate() though instead of bind().