简体   繁体   中英

jQuery slideToggle only working on one div, and not others

I have a simple tab navigation system set up, and I wanted to code it to be cleaner and more modular.

I made a JSFiddle for you to try it out.

HTML:

<div class="col-4">
    <nav>
        <ul>
            <li id="tab_entry"      value="0"   class="navActive myPanelNavLi"><span class="fa fa-plus-circle"></span> Entry</li>
            <li id="tab_search"     value="1"   class="myPanelNavLi">Search</li>
        </ul>
   </nav>

<div class="panelTab_0 myPanelActive lightShadow darkBg roundedBottom">
STORAGE ENTRY
</div>

<div class="panelTab_1 myPanelActive lightShadow darkBg roundedBottom">
STORAGE ENTRY
</div>

JS:

    //myPanel sliders
$(".myPanelNavLi").click(function(){

    var choiceValue = $(this).attr("value");
    var choice = '.panelTab_' + choiceValue;
    console.log(choice);

    $(this).parent().parent().next(choice).slideToggle(300);

});

Basically, when you click "Entry" it slide toggles properly,

But when you click Search, it doesn't slide. This is has been driving me crazy. Any help is greatly appreciated.

https://jsfiddle.net/g44xt4m8/3/

Thank you. Nick

try https://jsfiddle.net/g44xt4m8/5/

$(".myPanelNavLi").click(function(){

        var choiceValue = $(this).attr("value");
        var choice = '.panelTab_' + choiceValue;

        $(choice).slideToggle(300);


    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM