jQuery Forum

Why is unbind('click') not working?

by 
 on 23-Jan-2010 07:34 AM.
  in  Using jQuery 

Hi all,

I have the following piece of jQuery:

  1. $("img[alt='47767']").unbind('click').attr('src', '../images/icon-tick.png').attr('title', 'Printed');
Which I am trying to use to manipulate the following piece of HTML

  1. <img alt="47767" title="Mark as printed" style="cursor: pointer;" onclick="mark_printed(this)" src="../images/icon-printer.png">
The attribute changes for 'src' and 'title' get applied, but the unbind('click') doesn't stop the image from firing the 'mark_printed(this)' function.

Why is unbind('click') not working as expected?

Thanks!

David

  • No status
  • Working on it
  • Answered
  • Need more info
  1 user has this question 

Re: Why is unbind('click') not working?

by 
 on 23-Jan-2010 11:13 AM

Hello,


"onclick" is not realy an event, it's a dom attribute which you can remove with attr('onclick', '')


To use "unbind" function, you have to drop your onclick attribute and use "bind", "click", or better "one" function

  1. <style type="text/css">
  2. .mark_print{cursor: pointer;}
  3. </style>
  4. <script type="text/javascript">
  5.     $("img.mark_print").one('click', function(){
  6.         $(this).
  7.             attr('src', '../images/icon-tick.png').
  8.             attr('title', 'Printed').
  9.             removeClass('mark_print');
  10.     });
  11.  </script>

  12. <img alt="47767" title="Mark as printed" class="mark_print" src="../images/icon-printer.png">

Post Actions
Statistics
  • 1
     Replies
  • 34
     Views
  • 1
     Followers
Tags for the post
No tags available for this topic.

Edit Link Delete Link

Edit Link Delete Link

LoadingImage