Skip to content

Instantly share code, notes, and snippets.

@elijahmanor
Created February 22, 2012 18:19
Show Gist options
  • Save elijahmanor/1886465 to your computer and use it in GitHub Desktop.
Save elijahmanor/1886465 to your computer and use it in GitHub Desktop.
Find the jQuery Bug #5: Problem
(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 ...
<!-- Intercept click event and confirm before proceeding -->
<button
data-confirm-text="You sure you want to submit?">
Submit with Custom Prompt</button>​
(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