tags:

views:

75

answers:

3

Should work same as it's working independently.

This

(function($) { 
     $(document).ready(function(){
          $('#number1').hide();
        $('.button1').click(function(){
              $('.table1').slideToggle();
        $(this).toggleClass("minus_icon"); 
                return false;
   });
});

})(jQuery);

and this

(function($) { 
     $(document).ready(function(){
          $('#number2').hide();
        $('.button2').click(function(){
              $('.table2').slideToggle();
        $(this).toggleClass("minus_icon"); 
                return false;
   });
});

})(jQuery);

both will be used on same page.

Thanks

Edit: Added after @Felix comment

@Felix - Do you mean like this?

(function($) { 
     $(document).ready(function(){
          $('#number2, #number1').hide();
        $('.button2').click(function(){
              $('.table2').slideToggle();
        $(this).toggleClass("minus_icon"); 
                return false;
   });
});
        $('.button1').click(function(){
              $('.table1').slideToggle();
        $(this).toggleClass("minus_icon");
                return false;
   });
})(jQuery);
+1  A: 

A sample HTML

<button id="button1" class="btn" div="table1">Table 1</button>
<button id="button2" class="btn" div="table2">Table 2</button>
<div class="myDiv" id="div1"><table><tr><td>Table1</td></tr></table></div>
<div class="myDiv" id="div2"><table><tr><td>Table2</td></tr></table></div>

CSS

  .myDiv { display:none; }

Jquery

 $(document).ready(function(){

    $('.btn').click(function(){
        //identify same class to all your div for example in this case I will define all tables as myDiv
        // doing this will not fix the effect to just two tables
        $(".myDiv").slideUp(); //Hide all divs first

        $('#'+$(this).attr("div")).slideToggle(); //show the required
        $(this).toggleClass("minus_icon");
        return false;
    });
});
Starx
I also applied the suggestion of @Felix Kling
Starx
@metal gear solid, to combine both click function, you need to identify them by one fixed class and then pass the table name as attribute somewhere in you `buttons`See the update
Starx
@Strax - I have 2 link and 2 table on same page. and default i want to keep both table hidden . and if user will click on link "table1" then table1 should expand and when user will clik on link "table2" then table1 should be hidden and table1 should expand.
metal-gear-solid
@metal-gear-solid, you mispelled my name. But the answer is still the same, it will slide all the tables and show the tables you want later. May be you need accordion check this http://docs.jquery.com/UI/Accordion
Starx
@Starx - sorry, I have `table` inside `div`
metal-gear-solid
@metal-gear-solid, again its the same answer. First try it
Starx
+1 for great effort @Starx :)
Mattis
@Starx - not working see here http://jsfiddle.net/PYcer/1/
metal-gear-solid
That's your script, use the one I provided
Starx
it's ur script . first code of ur answer
metal-gear-solid
I noticed you didn't see the UPDATED portion of my answer so here is your solution http://jsbin.com/isopa. And I have updated my answer as well.
Starx
your example is working now. but , see my 2nd comment of your answer "I have `table` inside `div`"
metal-gear-solid
See new revision http://jsbin.com/isopa/2
Starx
I want to hide and show whole `div`, not only `table`
metal-gear-solid
@metal-gear-solid, this is a basic HTML dude, wrap the table in a div. See my answer again , I will modify it
Starx
`div` is not a valid attribute on a button HTML element. I would not introduce custom attributes.
Felix Kling
@Felix kling, you are right, and I agree that one should avoid this as much as possible
Starx
+1  A: 
(function($) { 
     $(document).ready(function(){
        $('#number1, #number2').hide();

        $('.button1, .button2').click(function(){
            if($(this).is('.button1')) {
                $('.table1').slideToggle();
            else
                $('.table2').slideToggle();
            $(this).toggleClass("minus_icon"); 
            return false;
        });

    });
})(jQuery);
rob waminal
@rob - thanks - and i also need if i clicked on table1 first then if i click on table2 then table1 should be hidden and table1 should expand
metal-gear-solid
@metal-gear-solid, see my answer
Starx
@Starx, you answer is close but you the call to `$($(this).attr("table")).slideToggle();` will not work it should be `$('.'+$(this).attr("table")).slideToggle();` since table1 and table2 are classes not tags
rob waminal
@rob thanks for the correction
Starx
A: 

It seems, that you have 2 repetitive markups, which should have same behavior. In that case, I would extract JS code, which defines required behavior, and call it with required markup arguments:

(function($) { 
    function CondfigureEvents(numberSelector, buttonSelector, tableSelector)
    {
        $(document).ready(function(){
        $(numberSelector).hide();
        $(buttonSelector).click(function(){
            $(tableSelector).slideToggle();
            $(this).toggleClass("minus_icon"); 
            return false;
        });
    }
    CondfigureEvents('#number1', '.button1', '.table1');
    CondfigureEvents('#number2', '.button2', '.table2');
});
})(jQuery);
Valera Kolupaev