Created
February 22, 2012 18:19
-
-
Save elijahmanor/1886465 to your computer and use it in GitHub Desktop.
Find the jQuery Bug #5: Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function( $ ) { | |
| $.fn.confirmAction = function() { | |
| return this.each( function() { | |
| var $this = $( this ), | |
| confirmText = $this.data( "confirm-text" ); | |
| $this.bind( "click.confirmAction", function( e ) { | |
| if ( confirmText ) { | |
| if ( confirm( confirmText ) ) { | |
| console.log( "Confirmed" ); | |
| } else { | |
| console.log( "Denied" ); | |
| e.preventDefault(); | |
| e.stopImmediatePropagation(); | |
| } | |
| } | |
| }); | |
| }); | |
| }; | |
| })( jQuery ); | |
| // ... more code ... | |
| // Run plugin against all the buttons on the page | |
| $( "button" ).confirmAction(); | |
| // Undo the plugin for the 1st button, modify data, & reinitialize | |
| $( "button:first" ) // Grab 1st button on the page | |
| .unbind( ".confirmAction" ) // Remove all namespaced event handlers | |
| .attr( "data-confirm-text", // Update changes to HTML5 data-attr | |
| "Are you really really sure you want to submit?" ) | |
| .confirmAction(); // Reinitialize plugin | |
| // ... more code ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- Intercept click event and confirm before proceeding --> | |
| <button | |
| data-confirm-text="You sure you want to submit?"> | |
| Submit with Custom Prompt</button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function( $ ) { | |
| $.fn.confirmAction = function() { | |
| return this.each( function() { | |
| var $this = $( this ), | |
| confirmText = $this.data( "confirm-text" ); | |
| $this.bind( "click.confirmAction", function( e ) { | |
| if ( confirmText ) { | |
| if ( confirm( confirmText ) ) { | |
| console.log( "Confirmed" ); | |
| } else { | |
| console.log( "Denied" ); | |
| e.preventDefault(); | |
| e.stopImmediatePropagation(); | |
| } | |
| } | |
| }); | |
| }); | |
| }; | |
| })( jQuery ); | |
| // Delegate click event on buttons to the document (similar to .live) | |
| $( document ).on( "click", "button", function() { | |
| alert( "Submit Form: Live" ); | |
| }); | |
| // Run plugin against all the buttons on the page | |
| $( "button" ).confirmAction(); | |
| // Undo the plugin for the 1st button, modify data, & reinitialize | |
| $( "button:first" ) // Grab 1st button on the page | |
| .unbind( ".confirmAction" ) // Remove all namespaced event handlers | |
| .attr( "data-confirm-text", // Update changes to HTML5 data-attr | |
| "Are you really really sure you want to submit?" ) | |
| .confirmAction(); // Reinitialize plugin | |
| // Bind click event on the buttons (similar to .bind) | |
| $( "button" ).on( "click", function() { | |
| alert( "Submit Form: Bind" ); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment