views:

81

answers:

4

Hello i have problem. I have that function:

$("#tabela tr").toggle(function(){
    $(this).addClass("zaznaczone");
    $(this).find(" :checkbox").attr("checked", "1");
},function(){
    $(this).removeClass("zaznaczone");
    $(this).find(" :checkbox").attr("checked", "");
});

and it's fine when i click on "tr" but when i click on checkbox this line response but checkbox not. It's mean checkobx after this operation is not checked.

A: 

the right value for checked is "on"!

Roki
U have right but 1, checked work too.Anyway it isn't solve my problem.
WooCaSh
correct value is `checked`
jAndy
@jAndy - In jQuery anything that's not `~= false` works, since it's normalized :)
Nick Craver
@Nick: I know you might even write `<input type="checkbox" checked/>`, but `w3c` says `checked` as correct value.
jAndy
@jAndy - Actually it doesn't, it specifies exactly `checked` not `checked="something"`, here's the spec: http://www.w3.org/TR/html401/interact/forms.html#adef-checked
Nick Craver
@Nick: well yea, I was confused by the table at the very beginning. I guess 95% of all "correct" codes are not that correct :). `disabled` should also not have a value. doh! Even jQuery would break the w3c spec, you can't tell `.attr()` just to add that attribute empty.
jAndy
A: 

i write bottom this code:

$("#tabela tr :checkbox").click(function(){
    if($(this).attr('checked')==''){
        alert("000000");
    }
    else{
        alert("111111111");
    }
});

and what happen. When i click on empty checkbox i see alert 1111111 and before press ok on alert popup checkbox is checked. After press ok checkbox is not checked and tr get class from code up. When i try uncheck checkbox i see this same alert 00000 and line "tr" is unset with code up

WooCaSh
+1  A: 

I wasn't able to make this work with .toggle(), it will call .preventDefault() on it's own which just makes this task awesome for some reason. Maybe I'm just missing something, but the code below is just fine and works.

$(document).ready(function(){
   $('tr').bind('click',function(e){
       var cb = $(this).find(':checkbox');

       if(e.target !== cb[0]){
           if(cb.is(':checked'))
               cb.removeAttr('checked');
           else
               cb.attr('checked', 'checked');
       }
   });   
});
jAndy
i will try it and back with answer
WooCaSh
@WooCaSh: does it work for you?
jAndy
like I answer: almost look answer up
WooCaSh
A: 

Your code work perfect for line and checkbox but when I try add this lines:

$(this).addClass("zaznaczone");
$(this).removeClass("zaznaczone");

then work that: When i click on line work that what it should, but when i click on checkbox then check it addClass dont work All code:

$('#tabela tr').bind('click',function(e){
       var cb = $(this).find(':checkbox');

       if(e.target !== cb[0]){
           if(cb.is(':checked')){
               cb.removeAttr('checked');
               $(this).removeClass("zaznaczone");}

           else{
               cb.attr('checked', 'checked');
               $(this).addClass("zaznaczone");}
       }
   });
WooCaSh