2

How do I optimize the code below? In less lines.

jQuery(document).ready(function(){
jQuery(".a1").hide();
jQuery(".a").click(function(){
    jQuery(".a1").toggle();
});

jQuery(document).ready(function(){
    jQuery(".b1").hide();
    jQuery(".b").click(function(){
        jQuery(".b1").toggle();
    });
    ...

...

Edit. Add my Html code.

<ul class="bg">
<li class="a">Text</li>
<li class="a1"><a href="#">Text</a></li>
<li class="a1"><a href="#">Text</a></li>
</ul>

<ul class="bg">
<li class="b">Text</li>
<li class="b1"><a href="#">Text</a></li>
<li class="b1"><a href="#">Text</a></li>
</ul>

Thanks for answers.

6
  • 1
    can you show as the corresponding html-markup? Commented Apr 8, 2015 at 11:04
  • 1
    please show your html code also. Commented Apr 8, 2015 at 11:04
  • Edit. I add html code. Commented Apr 8, 2015 at 11:11
  • sugest we move this post to code review Commented Apr 8, 2015 at 11:12
  • I'm voting to close this question as off-topic because it belongs on code-review. Commented Apr 8, 2015 at 11:17

5 Answers 5

1

use below code

DEMO

 jQuery(document).ready(function(){
   jQuery(".bg").find('li:not(:first)').hide();  // hide all li inside .bg expat  expat first.
   jQuery(".bg").find('li:first').click(function(){  // click event on first li
     $(this).nextAll('li').toggle(); // toggle other li expat first
   });
 });
Sign up to request clarification or add additional context in comments.

Comments

1

Try this : You can make use of siblings() to toggle lis

$(function(){
 $('.bg').find('li:first').siblings('li').hide();
  $('.bg').find('li:first').click(function(){
    $(this).siblings('li').toggle();
 });
});

JSFiddle Demo

Comments

0

You can try something like this:

jQuery(document).ready(function () {

    jQuery(".bg").find('li:has(a)').hide(); //hide all list-elements 
                                            //containing an anchor element

    jQuery(".bg > li:first-child").click(function () { //trigger click
                                                       // on the first list-element
        $(this).siblings('li').toggle(); //toggle all other list-elements
    });

});

It depends strongly on your provided html-markup, as it checks if there are a-tags inside the list-elements, which should be shown on click. But you can expand your existing markup with no need to change something or add additional data to it.

Demo

Reference

:has()

:first-child()

.siblings()

1 Comment

@user4758595 please have a look at your console: TypeError: $ is not a function, have a look at this article
0

There's no need for more than one jQuery(document).ready(function() { call.

jQuery(document).ready(function() {
  jQuery(".a1, .b1").hide();
  jQuery(".a").click(function() {
    jQuery(".a1").toggle();
  });
  jQuery(".b").click(function() {
    jQuery(".b1").toggle();
  });
});

2 Comments

click event is not on a1 b1 its .a .b.
repeat same pattern 20 times and you have to repeat code blocks 20 times. This solution isn't much of an improvement from OP code
0

You can do something like this:

<script>
$(function(){
 $('ul.bg li:first').siblings().hide();
 $('ul.bg li:first').on("click", function(){
  $(this).siblings().toggle();
 })
})
</script>

Try this and let me know if that helps you

3 Comments

what if a and b is up tp a50,b50 ? how to hide all that element?
Try this - $('ul.bg li:first').siblings().hide(); if that works
if that works, use the same selector $('ul.bg li:first').click()

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.