views:

57

answers:

1

Sorry guys, I am trying to add an event to a image inside of a table, so when its clicked it collapse the div under need.

I am facing several problems here.

  1. all tables and div use the same class.
  2. it may be a minimum of two tables and divs.
  3. first table should no be click able, only its images (for show and hide div under)
  4. rest of tables should be click able the whole table to show and hide div under with slidetoggle.
  5. rest of tables also have two images for show with slideDown and slideUp.

What I have it works but not fully.

Once again.

Thanks.

so far this is what I have.

<script type="text/javascript">
 $(document).ready(function(){

   $(".heading:not(#Container1)").click(function(){
     var c = $(this).next(".container");
     c.slideToggle("slow");
   });


 });

 $(document).ready(function(){
   $("img[alt='min']").click(function(){
     var c = $(this).next(".container");
     c.slideUp("slow");
   });

   $("img[alt='max']").click(function(){
     var c = $(this).next(".container");
     c.slideDown("slow");

   });
 });
</script>

<html>
  <head></head>
  <body>
    <table class="heading" id="container1">
      <tr>
        <td>heading1</td>
        <td><img alt='min'/><img alt='max'/></td>
     </tr>
   </table>
   <div class='container'>Container1</div>
   <table class="heading">
     <tr>
       <td>heading2</td>
       <td><img alt='min'/><img alt='max'/></td>
      </tr>
   </table>
   <div class='container'>Container2</div>
   <table class="heading">
     <tr>
       <td>heading3</td>
       <td><img alt='min'/><img alt='max'/></td>
     </tr>
   </table>
   <div class='container'>Container3</div>
 </body>
</html>
+3  A: 

Try this for the script:

$(function(){
   $(".heading:not(#Container1)").click(function(){
    $(this).next(".container").slideToggle("slow");
   });
   $("img[alt='min']").click(function(e){
     $(this).closest("table").next(".container").slideUp("slow");
     e.stopPropagation();
     return false;
   });
   $("img[alt='max']").click(function(e){
     $(this).closest("table").next(".container").slideDown("slow");
     e.stopPropagation();
     return false;
   });
});

SLaks also made a great catch, your script should be here:

<html>
 <head>
   //Script tags here
 </head>
Nick Craver
Also, put the script in the `<head>`
SLaks
@SLaks - Good catch, didn't even notice
Nick Craver
Wow!! that is pretty close, I am only having one issue.When I click on img it will slideUp or Down (depends which one you click) but it would also slideToggle, so it would go up and then down or down and then up when you click on image. Any solution for this? Thanks.
Cesar Lopez
@Cesar - Updated the answer, see if that does it for you.
Nick Craver
Thanks I already did that, but thanks anyway.
Cesar Lopez
@Cesar - You added the `.stopPropagation();` and `return false;` calls already?
Nick Craver
put an empty div with a width = the width of the image after the image, or around it. The table will "collapse vertically" but not horizontally, because the div will still be there.
egarcia
Ohh, I did not see that one, thanks, I going to try. :-)
Cesar Lopez
@Nick Craver:wow, you are a genius, Thank you very much, it worked like a dream. :-)
Cesar Lopez
Perhaps a little off topic, but `#Container1` should be `#container1`
patrick dw
@Cesar - Welcome, glad it's working for you!
Nick Craver