Search code examples
javascriptjqueryprototype-programming

Extending the Javascript prototype for all items of a certain class?


What is the best way to extend the Javascript prototype for all items of a certain class? I want all textboxes (input) of a certain class to have

function OnValueChange()
{
// Wow!
}

This way I can add onblur="OnValueChange" to my input.

I can do it on an individual object by modify the prototype, but I am wondering if there a way to it for all items of a certain class or other distinguishing attribute.

I have access to jquery in my project


Solution

  • If you have access to jQuery then just use :

    $(".inputClassNameHere").live("blur", function(){OnValueChange();});
    

    This way all current and future inputs with class inputClassNameHere will call OnValueChange when the blur event is triggered.