Victor
Victor

Reputation: 1271

ASP.NET radio button jQuery handling

I am trying to figure out why my page doesn't fire the radio button change event.

Here's the script I have for it, it's supposed to show a div once the radio button is checked.

        $("input[id$='radio1']").change(function () {
        if ($("input[id$=radio1]").is(':checked')) {
            $('#div1').removeClass('hidden');
        }
    });

what's wrong with this code?

Upvotes: 0

Views: 1563

Answers (4)

Royi Namir
Royi Namir

Reputation: 148714

$("input[id$='radio1']").change(function () {
    if ($(this).is(":checked")) {
        $("#div1").removeClass("hidden");
    }
});

Upvotes: 5

Joseph Marikle
Joseph Marikle

Reputation: 78570

$("input[id$='radio1']").change(function () {
    if ($(this).is(':checked')) {
        $('#div1').removeClass('hidden');
    }
});

You should use the this object in your function.

Upvotes: 3

JAiro
JAiro

Reputation: 6009

try asking in this way if the radio is checked.

if(document.getElementById('radio1').checked) {
    $('#div1').removeClass('hidden');
}

Upvotes: 0

Connell
Connell

Reputation: 14421

You need to use the this keyword inside the function, instead of asking jQuery to search for elements matching the selector again.

$("input[id$='radio1']").change(function () {
    if ($(this).is(':checked')) {
        $('#div1').removeClass('hidden');
    }
});

Upvotes: 1

Related Questions