views:

117

answers:

4
<tr id="tr99"><td>......</td></tr>

<input type="checkbox" onclick="toggletr(this);" value="val" id="cbox" />

The javascript:

$(document).ready(function() { 

function toggletr(obj)
{
    if(obj.checked) 
        $(#tr99).hide();
    else  
        $(#tr99).show();
}

hi.this is my code that runs in add page of staff. if user is in edit mode the value that value is checked in the code i mean to say in .cs . checkbox.checked ="true" means . that time i need to make that tr value "tr99" is visiable true if checkbox is not checked then make the tr as hide.

A: 

I think that you want this to happen

$(document).ready(function() { 

    function toggletr(obj){
        if(obj.checked){
            $("#tr99").show();
            $("#cbox").attr("value", "tr99");
        }else {
            $("#tr99").hide();
        }
    }

});

Is that it? You can also add the function directly

function toggletr(obj){
    if(obj.checked){
        $("#tr99").show();
        $("#cbox").attr("value", "tr99");
    }else {
        $("#tr99").hide();
    }
}
Victor
+1  A: 

Take the toggletr method out of the "$(document).ready(function() {"

<script type="text/javascript">
 function toggletr(obj){
  if(obj.checked)
   $('#tr99').hide();
  else
   $('#tr99').show();}
</script>
<tr id="tr99"><td>......</td></tr>
<input type="checkbox" onclick="toggletr(this);" value="val" id="cbox" />
Geoff
A: 

If I were you I'd set the onclick method to be an event handler:

$(function(){ 
$('#cbox').click(function(){
    if(this.checked){
        $("#tr99").show();
        $("#cbox").attr("value", "tr99");
    }else {
        $("#tr99").hide();
    }
});
});
CodeJoust
A: 

If you wanted to do pure jQuery and not mix in normal Javascript (obj.checked)....

$(function(){
    $("#cbox").click(function(){
     if($(this).is(":checked")){
      $("#tr99").show();
     }else{
      $("#tr99").hide();
     }
    });
});
WesleyJohnson
Why would you want to do that? `if (this.checked) {...}` is simpler, more efficient and more readable.
Tim Down
I agree, it is simpler, more efficient and readable. I was just trying to offer alternatives. Getting familiar with jQuery's is function helps for checking other things like visibility. If you're to be doing other checks using "is" in your code, then I would argue consistency is better than simplicity. Additionally, wrapping the check in jQuery then allows for method chaining.But again, I agree in this single case that "this.checked" is better.
WesleyJohnson