Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply slide toggle effect dynamically on click of all div

Tags:

html

jquery

css

When clicking on a specific li element - I want to be able to open it's respective div element.

I have the following code:

<ul class="no-padding pro-list">
    <li><a href="#" class="pro-1 pro">A</a>
        <div class="proc-description panel-1">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-2 pro">B</a>
        <div class="proc-description panel-2">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-3 pro">C</a>
        <div class="proc-description panel-3">
            <p>a</p>
            <p>b</p>
            <p>c</p>
        </div>
    </li>
    <li><a href="#" class="pro-4 pro">D</a>
        <div class="proc-description panel-4">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-5 pro">E</a>
        <div class="proc-description panel-5">
            Hello world! Lorem ipsum doner inut.
                        <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-6 pro">F</a>
        <div class="proc-description panel-6">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-7 pro">G</a>
        <div class="proc-description panel-7">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="pro-8 pro">H</a>
        <div class="proc-description panel-8">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>
                Lorem ipsum doner inut.
                    Lorem ipsum doner inut.
            </p>
        </div>
    </li>
    <li><a href="#" class="proc-9 ">I</a></li>
    <li><a href="#" class="proc-10 ">J</a></li>
    <li><a href="#" class="proc-11 ">K</a></li>
</ul>

It works fine when I add this script:

<script>
    jQuery(document).ready(function ($) {
        jQuery(".pro-2").click(function () {
            jQuery(".panel-2").slideToggle("slow");
        });
    });
</script>

However I want to make it dynamic. Instead of making so many functions for each a class and div class, I want to create 1 function that can toggle all li elements.

like image 430
Nehal Avatar asked May 27 '15 12:05

Nehal


2 Answers

You should bind event with a common class and then use traversal method to identify element to work on.

You can use already added pro class added to anchor and proc-description added to div.

Use

jQuery(document).ready(function($){
    jQuery("a.pro").click(function(){
       jQuery(this).next(".proc-description").slideToggle("slow");
   return false; 
  });            
});

jQuery(document).ready(function($){
    jQuery("a.pro").click(function(){
       jQuery(this).next(".proc-description").slideToggle("slow");
      
   });            
});
.proc-description {display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="no-padding pro-list">
    <li><a href="#" class="pro-1 pro">A</a>

        <div class="proc-description panel-1">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-2 pro">B</a>

        <div class="proc-description panel-2">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-3 pro">C</a>

        <div class="proc-description panel-3">
            <p>a</p>
            <p>b</p>
            <p>c</p>
        </div>
    </li>
    <li><a href="#" class="pro-4 pro">D</a>

        <div class="proc-description panel-4">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-5 pro">E</a>

        <div class="proc-description panel-5">Hello world! Lorem ipsum doner inut.
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-6 pro">F</a>

        <div class="proc-description panel-6">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-7 pro">G</a>

        <div class="proc-description panel-7">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="pro-8 pro">H</a>

        <div class="proc-description panel-8">
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut.</p>
            <p>Lorem ipsum doner inut. Lorem ipsum doner inut.</p>
        </div>
    </li>
    <li><a href="#" class="proc-9 ">I</a>
    </li>
    <li><a href="#" class="proc-10 ">J</a>
    </li>
    <li><a href="#" class="proc-11 ">K</a>
    </li>
</ul>
like image 102
Satpal Avatar answered Sep 21 '22 12:09

Satpal


Solution to this problem is here:

<script> 
       jQuery(document).ready(function($){
            jQuery("ul.pro-list a.pro").click(function(e){
               e.preventDefault();
                jQuery(this).next('div').slideToggle("slow");
           });

</script>

where e is event, and preventDefault() prevents you from anchor links, which means it doesn't redirects to the 'a links' and if it doesn't redirects to a link our toggle effect works.

like image 24
Nehal Avatar answered Sep 19 '22 12:09

Nehal