Using jquery to hide dynamically added divs

The jquery code below will hide divs ending in -value-wrapper when the page loads. My problem is that the Add More button will add another line (with an increased number). When this happens, all divs are expanded. Is there a way to keep the original hidden ones and hide them in a new set?

The html markup is as follows:

<div id="group-information-items">
    <fieldset>
        <legend>Member Information</legend>
        <label>Form label 1</label>
        <table>
            <tr>
                <td>
                <div class="form-radios">
                    <input type="radio" class="form-radio" checked="checked" value="0" name="gci[0][fa][value]" id="edit-gci-value-0"> A
                    <input type="radio" class="form-radio" value="1" name="gci[0][fa][value]" id="edit-gci-value-0">B 
                </div>

                <!-- This is the div that is hidden -->
                <div id="edit-gci-0-fa-list-value-wrapper" class="form-item">
                    <label>List: </label>
                    <textarea name="gci[0][fa_list][value]" rows="5" cols="60"></textarea>
                </div>

                </td>
            </tr>
        </table>

        <!-- WHEN THIS BUTTON IS CLICKED, A CLONE OF THE ROW ABOVE IS APPENDED TO THE TABLE -->
        <!-- THE ONLY DIFFERENCE IS [0] BECOMES [1] AND SO ON -->
        <div class="content-add-more clear-block">
            <input type="submit" value="Add more values" id="edit-gci-add-more" name="gci_add_more">
        </div>
    </fieldset>
</div>

The code that performs the initial hide is below:

$("#group-information-items div").each(function() {
        $("div[id$='-value-wrapper']").each(function() {
                        $(this).hide();
        });
});
+3
source share
2 answers

Firstly, if I am missing something, you can greatly simplify this hidden code:

$("#group-information-items").find("div[id$='-value-wrapper']").hide();

. , divs ? . :

div#group-information-items div.form-item { display: none; }

"" div . show() divs ( , , , ). , , live() delegate().

+1

divs :

  $("#edit-gci-add-more").click(function() {
             $("div[id$='-value-wrapper']").hide();
  });
0

Source: https://habr.com/ru/post/1756814/


All Articles