views:

92

answers:

4

Hi all, Can anybody help me letting me know what is wrong with the following code?

$(document).ready(function(){
    $("img").attr("alt")=='minimize'.click(function(){
      alert("he");
     });
});

thanks.

Addition: 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.slideToggle("slow");

   });

   $("img[alt='max']").click(function(){
   var c = $(this).next(".container");
   c.slideToggle("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: 
$(document).ready(function(){
    $("img[alt='minimize']").click(function(){
        alert("he");
    });
});

EDIT

$(function() {
    $('img[alt='min'], img[alt='max']').click(function() {
        var container = $(this).parent('table').next('div.container');

        if ( $(this).attr('alt') == 'min' )
            container.slideUp('slow');

        if ( $(this).attr('alt') == 'max' )
            container.slideDown('slow');

        return false;
    });

    $('table.heading:not(:first)').click(function() {
        $(this).next('div.container').slideToggle('slow');

        return false;
    });
});
aefxx
Don't sacrifice error-free code for speed in answering! -1
Boldewyn
+4  A: 

You need to use the attribute selector not get the attribute and compare it.

$(document).ready(function(){ 
    $("img[alt='minimize']").click(function(){ 
      alert("he"); 
     }); 
});
tvanfosson
Don't sacrifice error-free code for speed in answering! -1
Boldewyn
@Boldewyn - just exactly what do you think the error is? The inclusion of the single quotes? You know that they would be necessary if the alt text included spaces, don't you?
tvanfosson
Did you edit the answer in between? In the first version I saw in sec. 51 after asking the question, there was the second double quote missing (like this: `$("img[alt='minimize'])`).
Boldewyn
@Boldewyn - aefxx's answer was missing the double quote.
Greg
Of course I edited it -- it was missing the closing bracket. Unless the editor has intellisense you shouldn't be so quick to jump in and down vote because of a simple typo. If you notice a typo, just edit the answer and fix it (if you have enough rep) or comment that there is an error. Reserve your downvotes for answers that are actively wrong.
tvanfosson
@tvanfosson Here here!
Skilldrick
@Greg: True. Unfortunately, one cannot see the edit history of an answer, if it was edited by the author himself. And I saw an error in tvanfosson's answer, although I can't remember which (darn reload). @tvanfosson: I might be mistaken. If you say, that you didn't edit the answer meanwhile, I'll happily remove the -1.
Boldewyn
OK, I can't remove the downvotes. However, I donwvoted the *erroneous* answers (remember, I saw them in second 51...). So my bad conscience keeps in limits.
Boldewyn
@Boldewyn -- is my answer any less helpful because I edited it? In fact, was it unhelpful simply because it contained a typo that I quickly fixed. I took the time to answer the question -- not much because it was relatively easy -- then I also took time to double check my answer and correct a minor typo. I suspect I had it before you even finished typing your comment because I didn't see your comment until after I finished and had no idea what you were talking about. You can remove your downvote or not, doesn't matter to me, but I think you were a little overeager with it.
tvanfosson
@tvanfosson - how are you ever going to build a high reputation score if you keep making tiny errors? ...oh, wait...
patrick dw
+1  A: 

It is not syntactically correct. What are you trying to do? Maybe this?

$(document).ready(function(){
  $("img[alt=minimize]").click(function(){
    alert("he");
   });
});
Pointy
Ah! The first one who took the time to write it error-free. +1
Boldewyn
Thanks, friend!
Pointy
+2  A: 

The alt attribute is not a way to filter your images. It is designed to put alternative content for when the image cannot be displayed (not found, unwanted by user, no screen, etc.)

You should instead use the class attribute to discriminate your images the way you want.

Your code then becomes:

HTML

<img src="..." class="minimize" alt="A beautiful image">

Javascript

$(document).ready(function(){
    $("img.minimize").click(function(){
        alert("he");
    });
});
Vincent Robert
+1 While not directly answering the question , this is a good point. A class would, generally, be a better way to handle it, though I can imagine circumstances where you might want to select on `alt` -- say if you want to convert the alt text to a js-based tooltip.
tvanfosson