Search code examples
javascriptjqueryrepeateradvanced-custom-fields

Show a Hidden Field When One Menu Item is Clicked - Advanced Custom Fields Repeater


I have a disclaimer div that I want to show when a certain menu item is clicked. I have my Jquery correctly showing my hidden divs with this script: $(function(){ var $allItems = $(".food-container > div"); $(document.body).on("click", "a.menu-item", function () { var id = this.id, itemId = ".food-container > #item-" + id; $allItems.not($(itemId).fadeToggle()).hide(); }); }); I am using Advanced Custom Fields Repeater Field, so I need to hide the field out of the Repeater loop. The menu id that I have is "4". So I just need to connect the disclaimer div which is "burger-disclaimer" to the #4 click event.

Here is the menu: https://jsfiddle.net/42gqu9r9/


Solution

  • This should put you on the right path. I am grabbing the ID of the click event (evt variable), to see what was selected.

    HTML:

    <a id="1" href="javascript:void(0)" class="menu-item">Appetizers<i class="fa fa-caret-right"></i></a>
    <a id="2" href="javascript:void(0)" class="menu-item">Tex-Mex<i class="fa fa-caret-right"></i></a>
    <a id="3" href="javascript:void(0)" class="menu-item">Salads<i class="fa fa-caret-right"></i></a>
    <a id="4" href="javascript:void(0)" class="menu-item">Burgers<i class="fa fa-caret-right"></i></a>
    <div class="food-container">
        <div id="item-1" style="display: none;">
            <div class="food">
                <h3>Mac N Cheese Bites</h3> Breaded, then fried macaroni, stuffed with cheese</div>
            <div class="price"> $8 </div>
        </div>
        <div id="item-2" style="display: none;">
            <div class="food">
                <h3>Salads</h3> Salads
            </div>
            <div class="price"> $8 </div>
        </div>
        <div id="item-3" style="display: none;">
            <div class="food">
                <h3>Tex-Mex</h3> Tex-Mex
            </div>
            <div class="price"> $8 </div>
        </div>
        <div id="item-4" style="display: none;">
            <div class="food">
                <h3>Burgers</h3> Burger
            </div>
            <div class="price"> $8 </div>
        </div>
    </div>
    <div class="burger-disclaimer">
        *All sandwiches and burgers are served with fries or potato chips. Side salad and onion rings available as side, add $2 upcharge
    </div>
    

    JS:

    $(function() {
        var $allItems = $(".food-container > div");
        $(document.body).on("click", "a.menu-item", function(evt) {
            var id = this.id,
                itemId = ".food-container > #item-" + id;
            $allItems.not($(itemId).fadeToggle()).hide();
            if (evt.target.id == 4) {
                $('.burger-disclaimer').toggle();
            } else {
                $('.burger-disclaimer').hide();
            }
        });
    });
    

    And your updated JS Fiddle: https://jsfiddle.net/6y0ymao8/