JustAnotherDeveloper
JustAnotherDeveloper

Reputation: 3199

Getting a radio button ID with jQuery

$(function () {
    $("#MyInputBox").keyup(function () {
        var nextChk = $(this).next(":radio:checked").attr('id'));
        alert(nextChk);
    });
});

What is the correct way to say "Get the ID of the next checkbox which is checked" Am I even close?

Upvotes: 3

Views: 5945

Answers (1)

user1106925
user1106925

Reputation:

Assuming your radio inputs are all siblings, you'd need to use .nextAll(), and then narrow down to the first match.

$(this).nextAll(":radio:checked:first").attr('id');

or

$(this).nextAll(":radio:checked").first().attr('id');

Or you could technically use .nextUntil() with .next().

$(this).nextUntil(":radio:checked").next().attr('id');

Also, I see that you're asking about checkboxes, but your code uses :radio, so I guess I don't know which one you actually want.

Upvotes: 6

Related Questions