jQuery Unbind() method to remove a attached event handler

This article shows how to use the JQuery unbind() method for HTML group div elements using JQuery scripting, HTML5 and CSS3.


The jQuery bind() method attaches an event handler to elements, whereas unbind() detaches an existing event handler from elements. Use basic HTML code to create the HTML elements. The following is the HTML code I used for this tutorial.

HTML

  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head></head>  
  5.   
  6. <body>  
  7.     <div>  
  8.         <div class="objects">  
  9.             <div class="obj obj-01"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  10.   
  11.             <div class="obj obj-02"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  12.   
  13.             <div class="obj obj-03"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  14.   
  15.             <div class="obj obj-04"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  16.   
  17.             <div class="obj obj-05"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  18.         </div>  
  19.     </div>  
  20. </body>  
  21.   
  22. </html>

The preceding HTML code defines the div element groups and inserts an image inside a child div element using an IMG tag.

CSS

Use CSS code to make the look and feel for all the HTML elements as we used before in the HTML part. The following is the CSS code I have used in this tutorial.
  1. body {  
  2.     background - color#00FFCC;  
  3. }   
  4. div img {  
  5.     border-radius:15px;  
  6.     max-width100px;  
  7.     max-height100px;  
  8. }  
  9. .obj { width:100pxheight:100pxmargin:10pxpadding5px;  
  10. }  
  11. .objects div { floatleft; }

Before adding the CSS code be sure it covers the entire browser display area. The preceding CSS code defines the entire HTML element design process.

jQuery

The first step in the jQuery scripting part is to include the required jQuery library reference in the head element of your page.
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 

The following is the complete jQuery and JavaScript code I used in this tutorial.
  1.   $(document).ready(function(){  
  2.     $('.obj').one('click'function() {  
  3.         $(this).unbind('click');  
  4.         $(this).fadeOut('slow');  
  5.         return false;  
  6.     })  
  7. }); 

The preceding jQuery scripting code improves the performance of the application and it defines the entire HTML element UI functionality and effects.

Complete code

The following is the complete HTML, CSS and jQuery scripting codes I used in this tutorial.
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <style>  
  6.         body {  
  7.             margin50px 50px;  
  8.             padding0px;  
  9.             text-aligncenter;  
  10.             background-color#00FFCC;  
  11.         }  
  12.           
  13.         div img {  
  14.             border-radius: 15px;  
  15.             max-width100px;  
  16.             max-height100px;  
  17.         }  
  18.           
  19.         .obj {  
  20.             width100px;  
  21.             height100px;  
  22.             margin10px;  
  23.             padding5px;  
  24.         }  
  25.           
  26.         .objects div {  
  27.             floatleft;  
  28.         }  
  29.     </style>  
  30.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  31.     <script>  
  32.         $(document).ready(function() {  
  33.             $('.obj').one('click', function() {  
  34.                 $(this).unbind('click');  
  35.                 $(this).fadeOut('slow');  
  36.                 return false;  
  37.             })  
  38.         });  
  39.     </script>  
  40.   
  41.     <body>  
  42.         <div>  
  43.             <div class="objects">  
  44.                 <div class="obj obj-01"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  45.   
  46.                 <div class="obj obj-02"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  47.   
  48.                 <div class="obj obj-03"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  49.   
  50.                 <div class="obj obj-04"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  51.   
  52.                 <div class="obj obj-05"><img src='https://pbs.twimg.com/profile_images/601645996360138752/JXX6TiZa_400x400.png'></div>  
  53.             </div>  
  54.         </div>  
  55.     </body>  
  56.   
  57. </html> 

Output

1. Open the main Index html file in the web browser.

Main index page
                                                   Figure 1: Main index page

2. Press the div image element.

div image element
                                                      Figure 2: Unbind event

Unbind event
                                                        Figure 3: Unbind event

Conclusion

I hope you liked this useful article. It will be useful for jQuery beginners. Please provide your valuable feedback and suggestions.

Live demo link: Jsfiddle

References