0

I would like to hide td if input value is 0 but I can't get it to work?

http://jsfiddle.net/zxpsd8x6/1/

<td><input type="radio" name="hideifvalue0" value"0"></td>
<td><input type="radio" name="hideifvalue0" value"1"></td>

<button class="btn1">Hide</button>
<button class="btn2">Show</button>
<script>
 $(document).ready(function(){
 $(".btn1").click(function(){
 $("td").hide();
 });
 $(".btn2").click(function(){
 $("td").show();
 });
});
</script>
5
  • 7
    this html is invalid. Td should be the child of tabla and tr Commented Sep 23, 2014 at 14:05
  • 4
    And it should be value="0" not value"0". Are you looking for this jsfiddle.net/j08691/zxpsd8x6/9? Commented Sep 23, 2014 at 14:07
  • jsfiddle.net/zxpsd8x6/12 Commented Sep 23, 2014 at 14:10
  • Thx for your help, but can i also hide an class on the same command? I have a label stored in another td : jsfiddle.net/swcmp2ws/1 Commented Sep 23, 2014 at 15:36
  • The code in your jsfiddle should work... Commented Sep 23, 2014 at 15:55

3 Answers 3

1

Not the way I would recommend, but this fixes the selectors:

JSFiddle: http://jsfiddle.net/swcmp2ws/

$(document).ready(function () {
    $(".btn1").click(function () {
        $('td:has(input[value="0"])').hide();
               //or
        $('input[value="0"]').parent().hide();
    });
    $(".btn2").click(function () {
        $('td:has(input[value="0"])').show();
               //or
        $('input[value="0"]').parent().show();
    });
});

Notes:

  • Your HTML was invalid - added table and tr elements
  • You should just use classes on common elements for simpler selectors (e.g. class="hidethese") and $('.hidethese').hide()
Sign up to request clarification or add additional context in comments.

9 Comments

The question was how to hide the TD if the input value was zero, not if it had a particular name. Your code would actually incorrectly hide both TDs.
This is still wrong. It needs to check if value="0" not if value="hideifvalue0"
the question is poorly worded, but I'm guessing the name on the input is only there for illustrative purposes.
@evilunix And the purpose of the hide/show buttons is? :)
To hide the table cells who's child inputs have a value of zero. Why - who knows!
|
0

Change your HTML to:

<td><input type="radio" name="hideifvalue0" value="0"></td>
<td><input type="radio" name="hideifvalue0" value="1"></td>

Then your jQuery should be:

 $(document).ready(function(){
   $(".btn1").click(function(){
     $("input[value='0']").parent().hide();
   });
   $(".btn2").click(function(){
     $("input[value='0']").parent().show();
   });
});

2 Comments

What if there is one more input type= text. then this code will effect that input as well .. Isn't it ?
Yes - it would, but given the example HTML, it shouldn't be a problem. We only need to have a selector as specific as is needed to solve the problem at hand.
0

First of all your html elements are wrong. <td> should be inside <table><tr> like: <table> <tr> <td><input type="radio" name="hideifvalue0" value="0" /></td> <td><input type="radio" name="hideifvalue0" value="1" /></td> </tr> </table>

<button class="btn1">Hide</button> <button class="btn2">Show</button>

Then here your jQuery code goes:

$(".btn1").on('click', function () { $("input:radio[value='0']").parent().hide(); });

$(".btn2").on('click', function () { $("input:radio[value='0']").parent().show(); });

2 Comments

There is no need to iterate over the inputs, checking the value, when this can be incorporated into the actual jQuery selector.
Please check it and then down grade it. I have changed it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.