1

I want to display contact information by choosing different checkbox, like this picture:

enter image description here

Here is my code:

$(document).ready(function() {
  $('input[type="checkbox"]').on('change', function() {
    $('input[name="' + this.name + '"]').not(this).prop('checked', false);
    $(this).closest('div').css("display", "block");
  });
});	
.receipt_info{
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
  <tbody>
    <tr>
      <td>
        <input type="checkbox" name="receipt[]"/>
      </td>
      <td>Option1
        <div class="receipt_info">
          <div>
            <label>name1</label>
          </div>
          <div>
            <label>phone1</label>
          </div>
          <div>
            <label>address1</label>
          </div>
        </div>
      </td>
    </tr>

    <tr>
      <td>
        <input type="checkbox" name="receipt[]"/>
      </td>
      <td>Option2
        <div class="receipt_info">
          <div>
            <label>name2</label>
          </div>
          <div>
            <label>phone2</label>
          </div>
          <div>
            <label>address2</label>
          </div>
        </div>
      </td>
    </tr>
  </tbody>
</table>

I think I'm selecting wrong div, but I don't know how to fix it.

1
  • Please try answer I provided below Commented Nov 21, 2016 at 9:40

3 Answers 3

1

Through your div and the checkbox are seperated by td you should use jQuerys parents to get the parent tr and from there start searching for the corresponding receipt_info.

Like so:

$(this).parents('tr').find('.receipt_info').show();

I edited your jsfiddle here.

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

1 Comment

Thank, it's a quick answer.
1

Try this,

You need to first find the parent tr in which the required div is,

 $(document).ready(function() {
  $('input[type=checkbox]').on('change', function() {
    if ($(this).is(':checked')) {
      $(this).closest('tr').find('div.receipt_info').css("display", "block");
    }
    else
    {
     $(this).closest('tr').find('div.receipt_info').css("display", "none");
    }
  });
});

Fiddle : https://jsfiddle.net/9n9pfhe5/1/

1 Comment

Thanks, I forgot to say that the contact info should hide, too.
0

Use $(this).closest('tr').find('div.receipt_info') selector!

  • this refers to element on which event is invoked.
  • jQuery.closest will traverse up in DOMTree and returned matched element.
  • jQuery.find will find all the child of he current element.

$(document).ready(function() {
  $('input[type="checkbox"]').on('change', function() {
    $('input[name="' + this.name + '"]').not(this).prop('checked', false);
    $('.receipt_info').hide();
    $(this).closest('tr').find('div.receipt_info').show();
  });
});
.receipt_info {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
  <tbody>
    <tr>
      <td>
        <input type="checkbox" name="receipt[]" />
      </td>
      <td>Option1
        <div class="receipt_info">
          <div>
            <label>name1</label>
          </div>
          <div>
            <label>phone1</label>
          </div>
          <div>
            <label>address1</label>
          </div>
        </div>
      </td>
    </tr>

    <tr>
      <td>
        <input type="checkbox" name="receipt[]" />
      </td>
      <td>Option2
        <div class="receipt_info">
          <div>
            <label>name2</label>
          </div>
          <div>
            <label>phone2</label>
          </div>
          <div>
            <label>address2</label>
          </div>
        </div>
      </td>
    </tr>
  </tbody>
</table>

Comments

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.