ankur kumar
ankur kumar

Reputation: 427

check box code not working

<script>
function edit(em) {
    var ch = em.value;

    var ed = $("td.td" + ch).value;
    if ($(ed).is(: checked)) {
        $(this).show();
    } else {
        $(this).hide();
    }
}
</script>
   
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>        
</head>
<body>
    <input type="checkbox" value="25" onclick="edit(this)">
    <input type="checkbox" value="26" onclick="edit(this)">
    <input type="checkbox" value="27" onclick="edit(this)">
    <table>
        <tr>
            <td class="td25" value="25"><a href="#">Edit</a></td>
            <td class="td26" value="26"><a href="#">Edit</a></td>
            <td class="td27" value="27"><a href="#">Edit</a></td>
        </tr>
    </table>
</body>
</html>

Upvotes: 0

Views: 57

Answers (3)

ketan
ketan

Reputation: 19341

Following is what you are trying to do.

$('input[type="checkbox"]').click(function() { 
  var ch = this.value;
    if ($(this).is(':checked')) {
         $(".td" + ch).hide();
      }
     else
       {
         $(".td" + ch).show();
       }
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>        
</head>
<body>
    <input type="checkbox" value="25" >
    <input type="checkbox" value="26" >
    <input type="checkbox" value="27" >
    <table>
        <tr>
            <td class="td25" value="25"><a href="#">Edit25</a></td>
            <td class="td26" value="26"><a href="#">Edit26</a></td>
            <td class="td27" value="27"><a href="#">Edit27</a></td>
        </tr>
    </table>
</body>
</html>

Upvotes: 1

Samurai
Samurai

Reputation: 3754

This is what I think you're trying to do:

function edit(em) {
    var ch = em.value;

    var ed = $("td.td" + ch);
    if ($(em).is(':checked')) {
        ed.show();
    } else {
        ed.hide();
    }
}

If that's what you're trying to achieve, then here are things you weren't doing right:

  • As @yangguang said, you were missing quotes for checked (':checked')
  • You wanted to take td not its value.
  • You were checking whether td (or its value) is checked which instead you should have done it for the checkbox.
  • $(this) refers to the checkbox, but you wanted to show/hide the td

Upvotes: 0

Yangguang
Yangguang

Reputation: 1785

here is a bug:

if($(ed).is(:checked))...

shoul be:

if($(ed).is(':checked'))...

Upvotes: 1

Related Questions