Reputation: 3199
I have a textbox, which upon a change in text, calls some Javascript.
Before this textbox, I have 2 checkboxes (chkA, chkB) which are mutually exclusive. When I start my new function I want to be able to check which checkbox is currently selected. So far I have this,
$(".textbox1").keyup(function () {
var chk = $(this).prevAll(":checked").first().attr('class'); ....
});
The code goes on to do a "If class = blah, then do something, else, do nothing"
it worked for my radio button code, but not my checkboxes. "chk" ends up being null.
Upvotes: 0
Views: 989
Reputation: 25081
"I have 2 checkboxes (chkA, chkB) which are mutually exclusive." The purpose of using checkboxes is to allow the user to make choices that are mutually inclusive. You may want to reconsider and use radio buttons (which are designed to be are mutually exclusive).
That said, your handler for keyup
appears to be setting the class attribute of the closest (to the textbox) preceding element that has a checked attribute set to true. Do your checkboxes (chkA, chkB) meet that criteria, or are there other checkboxes after them (but before your textbox) that are checked?
Upvotes: 1
Reputation: 76880
If you need to know the class of a checked checkbox (i used an imaginary element that i called "wrapper" which holds all your input fields, substitute that with the correct element or post your markup):
$(".textbox1").keyup(function () {
if($(this).closest('#wrapper').find("input:checkbox:checked").hasClass('blah')){
//do what you need
});
Upvotes: 2