0

So when i tick a checkbox i want an element to show and then hide when unchecked.

                <div class="hndle">
                    <table class="form-table">
                        <td class="left">
                            Environmental Findings
                        </td>
                        <td>
                            <input type="checkbox" class="check">
                        </td>
                    </table>
                </div>
                <div class="hndle">
                    <textarea></textarea>
                </div>
                <div class="hndle">
                    <table class="form-table">
                        <td class="left">
                            Recommendations
                        </td>
                        <td>
                            <input type="checkbox" class="check">
                        </td>
                    </table>
                </div>
                <div class="hndle">
                    <textarea></textarea>
                </div>

Okay so when the checkbox is clicked i want it to show the next div with the class hndle.(the one which has a textarea inside).

This is the jQuery code i have attempted with but no luck getting it working.

        $('.check').change(function(){
            if($(this).is(':checked')){
                $(this).parents('hndle').next().show();
            }else{
                $(this).parents('hndle').next().hide();
            }
        });
1
  • sorry i have actually got the . in my code i must have written it wrong Commented Jul 6, 2015 at 11:28

2 Answers 2

1

hndle is a class of the target ancestor element, so use class selector

$('.check').change(function() {
  $(this).closest('.hndle').next().toggle(this.checked);
});

$('.check').closest('.hndle').next().hide()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="hndle">
  <table class="form-table">
    <td class="left">
      Environmental Findings
    </td>
    <td>
      <input type="checkbox" class="check" />
    </td>
  </table>
</div>
<div class="hndle">
  <textarea></textarea>
</div>
<div class="hndle">
  <table class="form-table">
    <td class="left">
      Recommendations
    </td>
    <td>
      <input type="checkbox" class="check" />
    </td>
  </table>
</div>
<div class="hndle">
  <textarea></textarea>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Actually this worked for me after changing all the $ to jQuery
@MatthewSmart you can use the dom ready handler like jsfiddle.net/arunpjohny/LqrwkceL/2 to use $
1

You have incorrect selector to target next handle div element. hndle is classname of div.Thus you need to use class selector . with it:

$('.check').change(function(){
    $(this).closest('.hndle').next().toggle(this.checked);
}).change(); // trigger event to hide on load

Working Demo

1 Comment

@MatthewSmart: check the fiddle

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.