1

I had create a table with two method of toggle function, first toggle is toggle table row with its specific ID and second toggle is toggle all the table row. However, if i click on first toggle at first parent row, and then when i click on second toggle, the child of first parent will hide, where it suppose follow other parent show their child and then click again to hide the child. And if all child is shown, the image also should change from details_open.png become details_close.png

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="w3.css" rel="stylesheet" />

  <body>

    <body>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script>
        function toggle(thisname, image) {
          var path = image.src;
          var filename = path.match(/.*\/([^/]+)\.([^?]+)/i)[1];
          if (filename == 'details_open') {
            image.src = 'details_close.png';
          }
          if (filename == 'details_close') {
            image.src = 'details_open.png';
          }
          tr = document.getElementsByTagName('tr')
          for (i = 0; i < tr.length; i++) {
            if (tr[i].getAttribute(thisname)) {
              if (tr[i].style.display == 'none') {
                tr[i].style.display = '';
              } else {
                tr[i].style.display = 'none';
              }
            }
          }
        }



        $(function() {
          $('#btn').click(function(e) {
            e.preventDefault();
            $('.td1').toggle();
          });
        });
      </script>

      <input type="button" id="btn" style="color:red" name=btn value="Toggle Child Accessories">
      <table class="w3-table-all">
        <thead>
          <tr>
            <td onclick="toggle(1,open_1);">
              <center><img id="open_1" src="details_open.png" style="width:20px;cursor: pointer;"></center>
            </td>
            <td> A </td>
            <td> B </td>
          </tr>
        </thead>

        <thead>
          <tr 1=fred style='display:none;' class='td1' id='td1'>
            <td> </td>
            <td> C </td>
            <td> D </td>
          </tr>
        </thead>

        <thead>
          <tr>
            <td onclick="toggle(2,open_2);">
              <center><img id="open_2" src="details_open.png" style="width:20px;cursor: pointer;"></center>
            </td>
            <td> E </td>
            <td> F</td>
          </tr>
        </thead>

        <thead>
          <tr 2=fred style='display:none;' class='td1' id='td1'>
            <td> </td>
            <td> G </td>
            <td> H </td>
          </tr>
        </thead>
      </table>
    </body>

</html>

2 Answers 2

1

If i understand correcly your need, you just need to control your rows's visibility to force toggle to hide or show all with his parameter display http://api.jquery.com/toggle/#toggle-display

$('#btn').click(function(e) {
    e.preventDefault();

    var display = true;

    if ($('.td1:visible').length == $('.td1').length) {
        display = false;
    }

    $('.td1').toggle(display);
});

https://codepen.io/anon/pen/xJymRr?editors=1010

I hope this will help you

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

3 Comments

May i know how can swap the image from details_open.png become details_close.png according to the child toggle?
i try add some class in table and make changes in javascript part but it seen failed to swap the image codepen.io/wkt931029/pen/WKamXg
Something was wrong in the selector codepen.io/anon/pen/vaVqLX?editors=1010
0

Since you are using jQuery, you can use jquery toggleClass()to swap the images using your css classes. jquery toggleClass. also noticed you have double <body> tag

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.