Javascript – jQuery click(fn) won’t work with input element

event-handlingjavascriptjquery

I want to use jQuery to detect clicks on a bunch of radio buttons. They have all been assigned a css class, foobar, to detect them. Easy, right? Still, this code doesn't work:

$("input.foobar").click(function ()
{ 
    alert(this.id);
}
);

What wrong with the code above?

Does this.id really return the id of the current radio button?

EDIT: The HTML on the page is really borked (doesn't valdiate), so maybe that's the problem…

EDIT 2: The HTML is messed up beyond salvation. Will have to let it be and fix a workaround. May the HTML Gods forgive me!

Best Solution

$("input.foobar").change(function () {
    alert(this.id);
});