tags:

views:

113

answers:

5

I tried following code:

<script type="text/javascript">     
 $(document).ready(function(){ 
   $("image").hover(
      function () {
        $(this).append($("<span> ***</span>"));
      }, 
      function () {
        $(this).find("span:last").remove();
      }
    );

  });

<img  alt="" src="../../Content/Disp.gif" />

It did not work. even use $("img"). Does it mean hover not working for image?

+2  A: 

You can try this:

$("img").each(function(){
    $(this).hover(function(){
        $(this).after("<span>Foobar</span>");
    }, function(){
        $(this).find("span:last").remove();
    });
});
Nathan
A: 

You cannot append something to a selfclosing tag

<img src="lol.gif" alt="" /> is a self-closing tag.
You can only append to not self closing tags like <div></div>
Time Machine
+1  A: 

There are some problems in this code.
First the selector is not correct:
Wrong

$('image')

Correct

$('img')

Second, the img element cannot contains child elements as far as I know, so you cannot use the "find" command with it.

 $(document).ready(function(){ 
   $("img").hover(
      function () {
        $(this).append($("<span>***</span>"));
      }, 
      function () {
        $(this).find("span:last").remove(); // this not correct
      }
    );

  });
Marwan Aouida
A: 
    <script type="text/javascript">
    $(document).ready(function(){
     $('img').hover(function(){
      $('span').html('Mouse over image'); 
     });
     $('img').mouseout(function(){
      $('span').html('Mouse not over image'); 
     });
    });
</script>  

<img  alt="" src="http://stackoverflow.com/content/img/so/logo.png" />
<span>Mouse not over image</span>
Ben Shelock
A: 
<script type="text/javascript">     
 $(document).ready(function(){ 
   $("img").hover(
      function () {
        $(this).after("<span> ***</span>");
      }, 
      function () {
        $(this).next().remove();
      }
    );
  });
</script>

<img alt="" src="../../Content/Disp.gif" />

Please do read api.jquery.com, it's a rather quick read through as the library is kind of tiny.

svinto